簡體   English   中英

在IIS中托管OWIN / Katana時如何獲得WindowsPrincipal?

[英]How do I get the WindowsPrincipal when hosting OWIN/Katana inside IIS?

我正在嘗試使用Windows身份驗證運行由IIS托管的OWIN / Katana應用程序,但是無論我做什么,我似乎只得到了未經身份驗證的GenericPrincipal ,而不是相關的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正在管道中的某個時刻更改主體。

嘗試投射對象:

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

告訴我此行是否為您提供正確的身份,以及數據是否正確……如果不正確,則在身份驗證過程中您還有其他問題……。

我遇到了這個問題,就我而言,這是由於Web.config中有一個<authentication mode="None"/>造成的。 刪除該文件后,一切運行良好-OWIN返回了WindowsIdentity而不是GenericIdentity。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM