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

时间:2011-11-16 16:41:00

标签: c# authentication nancy

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

2 个答案:

答案 0 :(得分:23)

史蒂文写道,南希支持基本和形式认证。看看这两个演示应用,看看如何做到每个:https://github.com/NancyFx/Nancy/tree/master/samples/Nancy.Demo.Authentication.Formshttps://github.com/NancyFx/Nancy/tree/master/samples/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];
        };
    }
  }
}

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

    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);
    }

答案 1 :(得分:1)

我创建了一个示例表单auth Web应用程序,用户管理Nancy用于我自己的学习。如果你想玩它,它就在Github上。

https://github.com/GusBeare/Nancy-UserManager