我该如何与Nancy进行身份验证?
本文关键字:身份验证 Nancy | 更新日期: 2023-09-27 18:14:45
我开始为Nancy编写LoginModule,但我突然想到可能需要以不同的方式执行身份验证。在南希有公认的做事方式吗?我现在正在计划两个项目:web和json服务。
正如Steven所写的,Nancy支持基本和表单授权。看看这两个演示应用程序,看看如何做到这一点:https://github.com/NancyFx/Nancy/tree/master/samples/Nancy.Demo.Authentication.Forms和https://github.com/NancyFx/Nancy/tree/master/samples/Nancy.Demo.Authentication.Basic
在第二个演示中,这里有一个需要验证的模块:
namespace Nancy.Demo.Authentication.Forms
{
using Nancy;
using Nancy.Demo.Authentication.Forms.Models;
using Nancy.Security;
public class SecureModule : NancyModule
{
public SecureModule() : base("/secure")
{
this.RequiresAuthentication();
Get["/"] = x => {
var model = new UserModel(Context.CurrentUser.UserName);
return View["secure.cshtml", model];
};
}
}
}
和一个在请求管道中设置表单认证的引导程序片段:
protected override void RequestStartup(TinyIoCContainer requestContainer, IPipelines pipelines, NancyContext context)
{
// At request startup we modify the request pipelines to
// include forms authentication - passing in our now request
// scoped user name mapper.
//
// The pipelines passed in here are specific to this request,
// so we can add/remove/update items in them as we please.
var formsAuthConfiguration =
new FormsAuthenticationConfiguration()
{
RedirectUrl = "~/login",
UserMapper = requestContainer.Resolve<IUserMapper>(),
};
FormsAuthentication.Enable(pipelines, formsAuthConfiguration);
}
我用Nancy创建了一个带有用户管理的表单认证web应用程序示例,供我自己学习。如果你想玩它,可以在Github上找到。
https://github.com/GusBeare/Nancy-UserManager