我在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"));
};
}
}问题是,如果因为用户没有claim boss而不允许用户输入/admin,那么Nancy只会返回http status403和空白正文。
这正是我的应用程序的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);
}
}发布于 2015-04-17 20:00:05
您需要将403状态代码作为管道的一部分进行处理,然后向用户返回一个html响应。看一看http://paulstovell.com/blog/consistent-error-handling-with-nancy
https://stackoverflow.com/questions/25798894
复制相似问题