6

NHibernate カスタム コンテキスト (ICurrentSessionContext) を実装しました。このコンテキストでは、NHibernate セッションを挿入するため、呼び出しパターンごとにセッションを設定します。さて、現在ログインしているユーザーの userId を取得するインターセプターを作成しました。今私はこれを行います:

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 は UserId を受け取りますが、この userId を取得する方法 (どこから) がわかりません。

4

2 に答える 2

1

ユーザーが認証されている場合は、次を使用できます。

OperationContext.Current.ServiceSecurityContext.PrimaryIdentity.Name
于 2010-08-17T14:28:14.927 に答える
0

現在のユーザーが現在のスレッドのプリンシパルとして設定されていると思いますか?

もしそうなら、このようなものが必要です:

var userName = Thread.CurrentPrincipal.Identity.Name;

この記事には、役立つと思われる追加情報がいくつかあります。

于 2010-08-17T14:22:16.370 に答える