【问题标题】:How do I get the WindowsPrincipal when hosting OWIN/Katana inside IIS?在 IIS 中托管 OWIN/Katana 时如何获取 WindowsPrincipal?
【发布时间】:2015-06-30 06:08:50
【问题描述】:

我正在尝试使用 Windows 身份验证运行由 IIS 托管的 OWIN/Katana 应用程序,但无论我做什么,我似乎只得到一个未经身份验证的 GenericPrincipal,而不是相关的 @987654322 @。

我的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 正在计划中的某个时间点更改委托人。

【问题讨论】:

  • 向我们展示 web.config 的一部分
  • 有什么特别的部分吗?
  • 你应该有类似的东西: 我说的对吗?跨度>
  • 添加了 web.config 的一部分。

标签: c# authentication iis asp.net-web-api katana


【解决方案1】:

尝试投射对象:

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

告诉我这条线是否为您提供了正确的身份以及数据是否正确...如果不是,那么您在身份验证过程中遇到了其他问题...。

【讨论】:

  • 我已经试过了。 Identity 是未经身份验证的 GenericIdentity,而不是 WindowsIdentity。
【解决方案2】:

我遇到了这个问题,在我的情况下,这是由于位于 Web.config 中的&lt;authentication mode="None"/&gt;。在我删除它之后,一切正常 - OWIN 返回了 WindowsIdentity 而不是 GenericIdentity。

【讨论】:

    猜你喜欢
    • 2015-01-09
    • 1970-01-01
    • 1970-01-01
    • 2015-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    相关资源
    最近更新 更多