【问题标题】:Handling failed claim in Nancy在南希处理失败的索赔
【发布时间】:2015-04-17 20:00:04
【问题描述】:

我在 Nancy 中使用 RequiresClaims 机制,如下所示:

public class HomeModule : NancyModule
{
    public HomeModule()
    {
        Get["/"] = ctx => "<a href=\"/admin\">Go here</a>";

        Get["/admin"] = ctx =>
        {
            this.RequiresClaims(new[] { "boss" });    // this
            return "Hello!";
        };

        Get["/login"] = ctx => "<form action=\"/login\" method=\"post\">" +
            "<button type=\"submit\">login</button>" +
            "</form>";

        Post["/login"] = ctx =>
        {
            return this.Login(Guid.Parse("332651DD-A046-4489-B31F-B6FA1FB290F0"));
        };
    }
}

问题是如果因为用户没有声明boss 而不允许用户输入/admin,Nancy 只会以 http 状态 403 和空白正文进行响应。

这正是我的应用程序的 Web 服务部分所需要的,但我的应用程序的某些部分也需要 nancy 为用户构建页面。如何向用户展示更多信息?

这是我使用的用户映射器:

public class MyUserMapper : IUserMapper
{
    public class MyUserIdentity : Nancy.Security.IUserIdentity
    {
        public IEnumerable<string> Claims { get; set; }
        public string UserName { get; set; }
    }

    public Nancy.Security.IUserIdentity GetUserFromIdentifier(Guid identifier, Nancy.NancyContext context)
    {
        return new MyUserIdentity { UserName = "joe", Claims = new[] { "peon" } };
    }
}

这是我使用的引导程序:

public class MyNancyBootstrapper : DefaultNancyBootstrapper
{
    protected override void RequestStartup(
        Nancy.TinyIoc.TinyIoCContainer container, Nancy.Bootstrapper.IPipelines pipelines, NancyContext context)
    {
        base.RequestStartup(container, pipelines, context);

        var formAuthConfig = new Nancy.Authentication.Forms.FormsAuthenticationConfiguration
        {
            RedirectUrl = "~/login",
            UserMapper = container.Resolve<Nancy.Authentication.Forms.IUserMapper>()
        };

        Nancy.Authentication.Forms.FormsAuthentication.Enable(pipelines, formAuthConfig);
    }
}

【问题讨论】:

    标签: nancy


    【解决方案1】:

    您需要将 403 状态代码作为管道的一部分进行处理,然后向用户返回一个 html 响应。看看http://paulstovell.com/blog/consistent-error-handling-with-nancy

    【讨论】:

      猜你喜欢
      • 2015-07-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-19
      • 1970-01-01
      • 1970-01-01
      • 2016-09-06
      • 1970-01-01
      • 2021-10-10
      相关资源
      最近更新 更多