首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Nancy中处理失败的索赔

在Nancy中处理失败的索赔
EN

Stack Overflow用户
提问于 2014-09-12 08:23:29
回答 1查看 695关注 0票数 0

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

代码语言:javascript
复制
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"));
        };
    }
}

问题是,如果因为用户没有claim boss而不允许用户输入/admin,那么Nancy只会返回http status403和空白正文。

这正是我的应用程序的web服务部分所需要的,但在我的应用程序的某些部分,nancy应该为用户构造页面。如何向用户显示更具信息性的内容?

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

代码语言:javascript
复制
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" } };
    }
}

这是我使用的引导程序:

代码语言:javascript
复制
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);
    }
}
EN

回答 1

Stack Overflow用户

发布于 2015-04-17 20:00:05

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25798894

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档