Skip to content

Instantly share code, notes, and snippets.

@vadymberkut
Created March 28, 2017 09:08
Show Gist options
  • Save vadymberkut/00a4e635f135528d10c08f03e367e9c9 to your computer and use it in GitHub Desktop.
Save vadymberkut/00a4e635f135528d10c08f03e367e9c9 to your computer and use it in GitHub Desktop.
AspNetCore Auth config
COOKIE AUTH
//Auth
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AutomaticAuthenticate = true,
AutomaticChallenge = true,
LoginPath = new PathString("/Account/Login"),
Events = new CookieAuthenticationEvents()
{
OnRedirectToLogin = ctx =>
{
if (ctx.Request.Path.StartsWithSegments("/api") &&
ctx.Response.StatusCode == (int)HttpStatusCode.OK)
{
ctx.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
}
else if (ctx.Request.Path.StartsWithSegments("/api") &&
ctx.Response.StatusCode == (int)HttpStatusCode.Unauthorized)
{
ctx.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
}
else
{
//Redirect to login
//ctx.Response.Redirect(ctx.RedirectUri);
//Do not redirect to login - ALWAYS retutn 401
ctx.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
}
return Task.FromResult(0);
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment