2011-11-16 102 views
19

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

+1

不能确定什么你问 - 你到底在写什么是“以不同的方式”?开箱即用支持表单身份验证和基本身份验证。 –

+0

那么,对于南希的网站来说,表单效果很好。对于我的json服务,我写了自己的身份验证部分,在每次请求时检查api键。 –

回答

23

正如史蒂文写道,南希支持基本和形式auth开箱即用。看看这两个演示应用程序,看看如何做到每个: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]; 
     }; 
    } 
    } 
} 

和设置形式的引导程序代码段AUTH在请求管道:

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

此答案适用于由Nancy提供支持的网站。对于一项服务,Nancy仍然缺少一些东西。我已经提交了一个包含一个新的StatelessAuthentication片断的请求(https://github.com/NancyFx/Nancy/pull/650#issuecomment-6416528)。南希(至少对我来说)是一种非常棒的网络或服务提供商技术。 –

+0

@ByronSommardahl我看到你的拉请求现在是南希的一部分。太好了! –

相关问题