2010-08-17 12 views
6

Ho implementato il contesto personalizzato NHibernate (ICurrentSessionContext). In questo contesto, faccio l'iniezione della sessione di NHibernate, quindi ho impostato Session per call pattern. Ok, ora ho creato un intercettore che accetta userId dell'utente attualmente loggato. Ora faccio questo:come ottenere l'id utente autenticato da wcf in 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; 
} 

mio AuditLogInterceptor prende UserId, ma io non so come (da dove) per ottenere questo ID utente.

risposta

1

Se l'utente è autenticato è possibile utilizzare:

OperationContext.Current.ServiceSecurityContext.PrimaryIdentity.Name 
0

Suppongo che l'utente corrente sia impostato come principale sul thread corrente?

Se è così, qualcosa di simile a questo è quello che vi serve:

var userName = Thread.CurrentPrincipal.Identity.Name; 

Ci sono alcune informazioni aggiuntive in this article che può rivelarsi utile.