1

我有一個OWIN託管其運行作爲Network Service與由OWIN啓動類的配置方法,下面一行啓用WindowsAuthentication網頁API。ApiController.User.Identity和System.Security.Principal.WindowsIdentity給不同的用戶信息

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

    } 
} 
+1

用戶帳戶下這在IIS網絡API項目的運行比誰登錄到您的應用程序並調用API的用戶不同。這就是爲什麼你在那裏得到不同的價值。如果您想要了解調用Web API的用戶的詳細信息,那麼您應該使用「ApiController.User.Identity」。 –

+0

謝謝。那麼,如果我想在ActionFilter中獲得這些細節呢? –

+0

在Action中,你應該有權訪問controllerContext,或者你可以簡單地執行Request.User.Identity。 –

回答

2

這顯然這裏解釋 - https://msdn.microsoft.com/en-us/library/aa302377.aspx

ASP.NET提供下列主要和標識對象 實現:

  • WindowsPrincipal的WindowsIdentity對象代表誰一直 使用Windows身份驗證身份驗證的用戶。通過這些對象, 角色列表將自動從Windows組所屬的組中自動獲取到Windows用戶所屬的 。
  • 的GenericPrincipalGenericIdentity對象代表誰一直用戶使用 Forms身份驗證或其他自定義 認證機制認證。通過這些對象,通常從數據庫中以自定義方式獲取角色列表 。
  • FormsIdentityPassportIdentity對象代表誰已經 分別被驗證表單和Passport身份驗證 用戶。

下表說明,對於一個範圍的IIS認證 設置,即從每個 變量的該保持的IPrincipal和/或 的IIdentity對象獲得的結果的身份。下面的縮寫用於 在表中:

  • 的HttpContext = HttpContext.Current.User,它返回一個 的IPrincipal對象,它包含當前Web請求安全信息 。這是經過驗證的Web客戶端 。
  • 的WindowsIdentity = WindowsIdentity.GetCurrent(),它返回當前正在執行的Win32 線程的安全上下文的 身份。
  • 螺紋 = Thread.CurrentPrincipal中它返回騎在在Win32 螺紋的頂部的當前執行.NET線程的主要 。

注意      在IIS 6.0的Windows Server 2003上運行,該單位矩陣的作品不同的是,機\ ASPNET 身份被替換爲NT AUTHORITY \ Network服務。

enter image description here

+0

'HttpContext.Current'爲空。 'WindowsIdentity.GetCurrent()'提供服務帳戶的詳細信息。 Thread.CurrentPrincipal提供了必需的細節。細節選擇線程是否是好習慣? –

+0

@AjayAradhya - 你確定'HttpContext.Current'爲空嗎?可能你對Controller和System.Web.HttpContext.Current的HttpContext屬性感到困惑。 'Controller.HttpContext' - 控制器上下文在構造函數中不可用(null)。如果我沒有錯,你可以嘗試從'System.Web.HttpContext.Current.User.Identity.Name'得到身份名稱。' – Developer

+0

我得到'System.Web.HttpContext.Current'爲空。 –

相關問題