2010-08-17 14 views
6

Mam zaimplementowany niestandardowy kontekst NHibernate (ICurrentSessionContext). W tym kontekście wstrzykiwam sesję NHibernate, więc mam konfigurację Session per call. Ok, teraz zrobiłem przechwytywacz, który przyjmuje identyfikator użytkownika bieżącego zalogowanego użytkownika. Teraz to zrobić:jak uzyskać uwierzytelniony identyfikator użytkownika z wcf w nhibernate

public ISession CurrentSession() 
{ 
    // Get the WCF InstanceContext: 
    var contextManager = OperationContext.Current.InstanceContext.Extensions.Find<NHibernateContextManager>(); 
    if (contextManager == null) 
    { 
    throw new InvalidOperationException(
     @"There is no context manager available. 
     Check whether the NHibernateContextManager is added as InstanceContext extension. 
     Make sure the service is being created with the NhServiceHostFactory. 
     This Session Provider is intended only for WCF services."); 
    } 

    var session = contextManager.Session; 
    AuditLogInterceptor interceptor = new AuditLogInterceptor(); 
    if (session == null) 
    { 
    session = this._factory.OpenSession(interceptor); 
    interceptor.Session = session; 

    contextManager.Session = session; 
    } 

    return contextManager.Session; 
} 

Moja AuditLogInterceptor trwa userid ale nie wiem jak (skąd), aby uzyskać ten userid.

Odpowiedz

1

Jeśli użytkownik jest uwierzytelniony można użyć:

OperationContext.Current.ServiceSecurityContext.PrimaryIdentity.Name 
0

Zakładam, że bieżący użytkownik jest ustawiony jako główny w bieżącym wątku?

Jeśli tak, coś w tym jest to, czego potrzebujesz:

var userName = Thread.CurrentPrincipal.Identity.Name; 

Istnieje kilka dodatkowych informacji w this article, które mogą okazać się pomocne.