1

OWIN Startup クラスの Configuration メソッドの次の行で有効にし Network Serviceて実行される OWIN でホストされた Web API があります。WindowsAuthentication

HttpListener listener = (HttpListener)appBuilder.Properties["System.Net.HttpListener"];
listener.AuthenticationSchemes = AuthenticationSchemes.IntegratedWindowsAuthentication;

ユーザーの詳細を取得しようとする場合を除いて、すべて正常に動作します。

  • caller = System.Security.Principal.WindowsIdentity.GetCurrent();
    戻り値:AuthenticationType: "Negotiate", Name: "NT AUTHORITY\NETWORK SERVICE"
  • ApiController.User.Identity
    戻り値:AuthenticationType: "NTLM", Name: "Domain\Username"

私は実際にApiController.User.Identity与えられた資格情報を期待していました。両方で異なる結果が得られた理由について混乱しています。誰でもこれで私を助けることができますか?

public class CustomFilterAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(HttpActionContext actionContext)
    {
        var caller = OperationContext.Current; //null
        caller = System.Web.HttpContext.Current; //null
        caller = actionContext.RequestContext.Principal.Identity as WindowsIdentity; //desired
        caller = System.Security.Principal.WindowsIdentity.GetCurrent(); //gives account details under which the project is hosted. 
    }
}

OWIN スタートアップ クラス:

public class Startup
{
    public void Configuration(IAppBuilder appBuilder)
    {
         HttpConfiguration config = new HttpConfiguration();
         HttpListener listener = (HttpListener)appBuilder.Properties["System.Net.HttpListener"];
         listener.AuthenticationSchemes = AuthenticationSchemes.IntegratedWindowsAuthentication;

         config.MapHttpAttributeRoutes();
         config.MapODataServiceRoute(
                routeName: "ODataRoute",
                routePrefix: "Data",
                model: GetModel()
         );
         config.EnsureInitialized();
         appBuilder.UseWebApi(config);

    }
}
4

1 に答える 1

2

これはここで明確に説明されています - https://msdn.microsoft.com/en-us/library/aa302377.aspx

ASP.NET は、次のプリンシパルおよび ID オブジェクトの実装を提供します。

  • WindowsPrincipalおよび WindowsIdentityオブジェクトは、Windows 認証で認証されたユーザーを表します。これらのオブジェクトを使用すると、Windows ユーザーが属する一連の Windows グループからロール リストが自動的に取得されます。
  • GenericPrincipalおよび GenericIdentityオブジェクトは、フォーム認証またはその他のカスタム認証メカニズムを使用して認証されたユーザーを表します。これらのオブジェクトを使用すると、通常はデータベースからロール リストが独自の方法で取得されます。
  • FormsIdentityオブジェクトと PassportIdentityオブジェクトは、それぞれフォーム認証と Passport 認証で認証されたユーザーを表します。

次の表は、さまざまな IIS 認証設定について、IPrincipalオブジェクトや IIdentityオブジェクトを維持する各変数から取得される結果の ID を示しています。表では、次の略語が使用されています。

  • HttpContext = HttpContext.Current.User 。これは、現在の Web 要求のセキュリティ情報を含むIPrincipalオブジェクトを返します 。これは、認証された Web クライアントです。
  • WindowsIdentity = WindowsIdentity.GetCurrent()。現在実行中の Win32 スレッドのセキュリティ コンテキストの ID を返します。
  • Thread = Thread.CurrentPrincipalは、現在実行中の .NET スレッドのプリンシパルを返します。これは、Win32 スレッドの上に乗っています。

   Windows Server 2003 で実行されている IIS 6.0 では、ID マトリックスは機能しますが、Machine\ASPNET ID が NT Authority\Network Service に置き換えられます。

ここに画像の説明を入力

于 2017-03-06T05:10:01.397 に答える