Как пишет Стивен, Нэнси поддерживает базовую аутентификацию и авторизацию форм из коробки. Взгляните на эти два демонстрационных приложения, чтобы узнать, как это сделать: https://github.com/NancyFx/Nancy/tree/master/samples/Nancy.Demo.Authentication.Forms и https://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];
};
}
}
}
и фрагмент загрузчика, который устанавливает аутентификацию формы в конвейере запросов:
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);
}