【问题标题】:OWIN-hosted Nancy Module Calling RequiresAuthentication sends back 401OWIN 托管的 Nancy 模块调用 RequiresAuthentication 发回 401
【发布时间】:2016-04-20 20:07:36
【问题描述】:

我有一个像这样的 Nancy 模块。

public class HomeModule : NancyModule
{
    public HomeModule()
    {
        this.RequiresAuthentication();

        var context = this.Context; // this is null

        Get["/"] = x =>
        {
            return "Hello";
        };
    }
}

OWIN 中间件将 OwinRequest 对象的 User 属性设置为具有经过身份验证的身份的 ClaimsPrincipal。如果我打破模块内部并看到 Thread.CurrentPrincipal,它被正确设置为我之前在中间件中设置的ClaimsPrincipal。然而,RequriesAuthentication 发回 401。顺便说一句,模块内部的上下文为空。我做错了什么?

我使用的一些感兴趣的 NuGet 包是

<package id="Microsoft.Owin" version="1.1.0-beta2" targetFramework="net45" />
<package id="Microsoft.Owin.Host.HttpListener" version="1.1.0-beta2" targetFramework="net45" />
<package id="Microsoft.Owin.Hosting" version="1.1.0-beta2" targetFramework="net45" />
<package id="Nancy" version="0.17.1" targetFramework="net45" />
<package id="Nancy.Owin" version="0.17.1" targetFramework="net45" />
<package id="Owin" version="1.0" targetFramework="net45" />

【问题讨论】:

    标签: nancy owin katana


    【解决方案1】:

    RequiresAuthentication 基于 Nancy IUserIdentity 实现,那么您将需要另一种方法来验证您的 ClaimsPrincipal 身份。

    见此链接一瞥: http://dhickey.ie/post/2014/01/04/Introducing-NancyMSOwinSecurity.aspx

    代码如下: https://github.com/NancyFx/Nancy.MSOwinSecurity/blob/master/src/Nancy.MSOwinSecurity/NancyContextExtensions.cs

    【讨论】:

      【解决方案2】:

      在你的路由中访问 Context 属性,比如

      Get["/"] = x => "hello " + this.Context.CurrentUser.UserName;
      

      当前的 Nancy 身份验证系统是基于 IUserIdentity 类型,但 OWIN 类型是 ClaimsPrinciple,所以你需要一些方法来做桥接的事情,比如上面提到的包 MsOwinSecurity。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-09-22
        • 2015-06-12
        • 1970-01-01
        • 1970-01-01
        • 2015-06-01
        • 2020-09-16
        • 2017-07-31
        相关资源
        最近更新 更多