21

Nancy の LoginModule のコーディングを開始しましたが、おそらく別の方法で認証を実行する必要があることに気づきました。ナンシーで認証を行う受け入れられた方法はありますか? 現在、web と json サービスの 2 つのプロジェクトを計画しています。両方に認証が必要です。

4

2 に答える 2

24

Steven が書いているように、Nancy はすぐに使える基本認証とフォーム認証をサポートしています。それぞれの方法については、次の 2 つのデモ アプリをご覧ください。 Nancy/tree/master/samples/Nancy.Demo.Authentication.Basic

これらのデモの 2 番目から、認証を必要とするモジュールを次に示します。

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);
    }
于 2011-11-16T21:25:35.697 に答える