我该如何处理南希的身份验证?

Byr*_*ahl 20 c# authentication nancy

我开始为Nancy编写一个LoginModule,但我想到可能需要以不同的方式执行身份验证.在南希有一种可以接受的认证方式吗?我现在正在计划两个项目:web和json服务.我需要两个身份验证.

Chr*_*dal 23

正如史蒂文写道,南希支持基本和形式认证.看看这两个演示应用程序,看看如何做每个:https://github.com/NancyFx/Nancy/tree/master/samples/Nancy.Demo.Authentication.Formshttps://github.com/NancyFx/南希/树/主/样品/ Nancy.Demo.Authentication.Basic

从这些演示中的第二个是这个模块需要auth:

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];
        };
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

以及在请求管道中设置表单身份验证的引导程序代码段:

    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);
    }
Run Code Online (Sandbox Code Playgroud)

  • 这个答案是由南希提供支持的网站的答案.对于一项服务,南希仍然缺少一些东西.我已经提交了一个包含新的StatelessAuthentication文件的拉取请求(https://github.com/NancyFx/Nancy/pull/650#issuecomment-6416528).这种类型的身份验证使南希(至少对我来说)成为一种非常棒的网络或服务提供商技术. (8认同)