【发布时间】: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