こんにちは、ウェブサイトに問題があります。ログイン後、サイトからログインページにリダイレクトされ、この機能を使用する権利がないというメッセージが表示されることがあります。ログインしてください。
まず、アクセス権に何か問題があると思いましたが、そうではありません。ブラウザで 1 つのサイトに戻って、もう一度リクエストを送信すると、機能します。
だから私はそれをデバッグし、大量のテストの後、Ninjectでサービスコンテキストを作成すると、UserNameが空の文字列であることを発見しました。
ここに私のコードがあります: 最初の私のインターフェイス IServiceContext:
public interface IServiceContext
{
string UserName { get; }
}
次に、クラス ServiceContext:
public class ServiceContext : IServiceContext
{
private static Object _syncRoot = new Object();
private static long _instance = 0;
private long _currentInstance = 0;
public string UserName { get; private set; }
public ServiceContext()
{
lock (_syncRoot)
{
if (_instance >= long.MaxValue)
_instance = 0;
_currentInstance = _instance++;
}
if (System.Web.HttpContext.Current.User == null)
UserName = "";
else
UserName = System.Web.HttpContext.Current.User.Identity.Name;
}
public ServiceContext(string userName)
{
UserName = userName;
}
public override string ToString()
{
return string.Format("#{0}, UserName: {1}", _currentInstance, UserName);
}
}
私のバインディングは別のファイルに設定されており、次のようになります。
Bind<SecurityManagement >().ToSelf().InTransientScope();
Rebind<IServiceContext>().To<ServiceContext>().InRequestScope();
フレームワークで serviceContext の StandardBinding が作成されるため、再バインドを使用する必要があります。
そして、私の InvokeMethod からの呼び出し:
class SecurityManagement : IInterceptor
{
public void Intercept(IInvocation invocation)
{
IServiceContext _context = _kernel.Get<IServiceContext>();
String name = System.Web.HttpContext.Current.User.Identity.Name;
}
}
そのため、_context.UserName が空の文字列になることがあります。ServiceContext に注入すると System.Web.HttpContext.Current.User.Identity.Name プロパティが空の文字列であることがわかりましたが、変数名には正しいユーザー名があります。私が使用しているフレームワークのプロパティ UserName のセッターを公開することは、私にとってオプションではありません。
なぜこれが起こっているのか誰にも分かりますか?おそらく問題を解決するためのアイデア