Skip to content

Instantly share code, notes, and snippets.

@igorkulman
Last active May 5, 2016 16:56
Show Gist options
  • Save igorkulman/2430a948fe6c426cdd01 to your computer and use it in GitHub Desktop.
Save igorkulman/2430a948fe6c426cdd01 to your computer and use it in GitHub Desktop.
protected override void RequestStartup(TinyIoCContainer container, IPipelines pipelines, NancyContext context)
{
base.RequestStartup(container, pipelines, context);
var formsAuthConfiguration =
new FormsAuthenticationConfiguration()
{
DisableRedirect = true,
UserMapper = container.Resolve<IUserMapper>()
};
FormsAuthentication.Enable(pipelines, formsAuthConfiguration);
}
Post["/login"] = _ =>
{
var loginParams = this.Bind<LoginParams>();
var user = _userService.Get(loginParams.Username, loginParams.Password);
if (user == null)
{
return HttpStatusCode.Unauthorized;
}
var authResult = this.LoginAndRedirect(user.Guid);
return Response.AsJson(new
{
username = user.Username
}).WithCookie(authResult.Cookies.First());
};
Get["/logout"] = _ =>
{
this.RequiresAuthentication();
var response = this.LogoutWithoutRedirect();
return Response.AsText("logout").WithCookie(response.Cookies.First());
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment