【问题标题】:How should I handle authentication with Nancy?我应该如何处理与 Nancy 的身份验证?
【发布时间】:2011-12-30 14:48:45
【问题描述】:

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

【问题讨论】:

  • 不太确定你在问什么——你在写什么,什么是“不同的方式”?开箱即用支持表单身份验证和基本身份验证。
  • 嗯,对于 Nancy 的网站,表格效果很好。对于我的 json 服务,我编写了自己的身份验证片段,在每个请求时检查一个 api 密钥。

标签: c# authentication nancy


【解决方案1】:

正如 Steven 所写,Nancy 支持开箱即用的基本身份验证和表单身份验证。看看这两个演示应用程序,看看如何做:https://github.com/NancyFx/Nancy/tree/master/samples/Nancy.Demo.Authentication.Formshttps://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];
        };
    }
  }
}

以及在请求管道中设置表单身份验证的引导程序 sn-p:

    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 提供支持的网站。对于一项服务,南希仍然缺少一些东西。我已经提交了一个拉取请求 (github.com/NancyFx/Nancy/pull/650#issuecomment-6416528),其中包含一个新的 StatelessAuthentication 片段。这种类型的身份验证使 Nancy(至少对我而言)成为一种非常出色的网络或服务提供商技术。
  • @ByronSommardahl 我看到你的拉取请求现在是 Nancy 的一部分。不错!
【解决方案2】:

我使用 Nancy 创建了一个带有用户管理的示例表单身份验证 Web 应用程序,以供我自己学习。如果你想玩它,它在 Github 上。

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

【讨论】:

    猜你喜欢
    • 2013-08-05
    • 1970-01-01
    • 2023-04-10
    • 2010-12-05
    • 2019-08-31
    • 1970-01-01
    • 2018-03-10
    • 2014-06-14
    • 1970-01-01
    相关资源
    最近更新 更多