2015-04-22 34 views
1

我試圖運行由Windows身份驗證的IIS承載的OWIN/Katana應用程序,但無論我做什麼,我似乎都會得到未經過身份驗證的GenericPrincipal,而不是相關的WindowsPrincipal如何在IIS中託管OWIN/Katana時獲取WindowsPrincipal?

Startup.cs

public partial class Startup 
{ 
    public void Configuration(IAppBuilder app) 
    { 
     var config = new HttpConfiguration(); 
     WebApiConfig.Register(config); 
     app.UseWebApi(config); 
    } 
} 

我的控制器:

public class TestController : ApiController 
{ 
    [Authorize] 
    public string Get() 
    { 
     return "Test"; 
    } 
} 

不必看着通過代碼的要求和調試,它看起來好像NTLM身份驗證成功的情況發生,但是當它到達WebAPI,委託人未通過身份驗證,因此它返回401,導致IIS再次嘗試執行Windows身份驗證。

Web.config文件片段:

<system.web> 
<authentication mode="Windows" /> 
<compilation debug="true" targetFramework="4.5" /> 
<httpRuntime targetFramework="4.5" /> 

更新

看樣子WindowsPrincipal並使其成爲我添加了一個虛擬中間件,但不進的WebAPI本身。看起來Katana正在改變管道的某一點。

+0

向我們展示部分web.config – Dryadwoods

+0

任何部分? – ChrisPatrick

+0

你應該是這樣的: \t \t <授權> \t \t \t <拒絕用戶=/「?」> \t \t \t \t <認證模式= 「窗口」/> 上午我糾正? – Dryadwoods

回答

0

嘗試投的對象:

using System.Security.Principal; 

    public ActionResult Get() 
    { 
     var identity = this.User.Identity as WindowsIdentity; 

     if (identity != null) 
     { 

      var data = new WindowsUserInformation { 
          Name = identity.Name, 
          AccountDomainSid = identity.User.AccountDomainSid.Value, 
          CreationDate = DateTime.UtcNow 
     }; 

告訴我,如果這條線是給你正確的身份,如果數據是正確的......如果不是,那麼u有在其他一些問題身份驗證....

+0

我已經試過這個。身份是未經身份驗證的GenericIdentity,而不是WindowsIdentity。 – ChrisPatrick

0

我有這個問題,在我的情況下,這是由於坐在Web.config中的<authentication mode="None"/>。我刪除後,一切都很好 - OWIN返回了WindowsIdentity而不是GenericIdentity。