繁体   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