2010-08-17 108 views
6

我已经实现了NHibernate自定义上下文(ICurrentSessionContext)。 在这种情况下我注入NHibernate会话,所以我有Session每个调用模式设置。 好的,现在我做了一个截取当前登录用户的userId的拦截器。 现在我这样做:如何从nhibernate中的wcf获取经过身份验证的用户标识

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; 
} 

我AuditLogInterceptor需要用户ID,但我不知道如何(从那里)得到这个用户ID。

回答

1

如果您的用户进行身份验证,您可以使用:

OperationContext.Current.ServiceSecurityContext.PrimaryIdentity.Name 
0

我假设当前用户被设置为当前线程的主体?

如果是这样,这样的事情是你所需要的:

var userName = Thread.CurrentPrincipal.Identity.Name; 

有在this article一些额外的信息,可能证明是有益的。

相关问题