Skip to content

Instantly share code, notes, and snippets.

@Legogris
Last active November 16, 2016 19:44
Show Gist options
  • Select an option

  • Save Legogris/ba36ea8ecd67afc548103a18cb1411a7 to your computer and use it in GitHub Desktop.

Select an option

Save Legogris/ba36ea8ecd67afc548103a18cb1411a7 to your computer and use it in GitHub Desktop.
public static void UseCactiCookieAuthentication(this IApplicationBuilder app, IHostingEnvironment environment)
{
var cookieName = "AuthCookie";
app.Use(next => context =>
{
context.Response.OnStarting(state =>
{
if(context.Items.ContainsKey("dontRenewAuthCookie"))
{
var response = (HttpResponse) state;
// Omit Set-Cookie header with the offending cookie name
var cookieHeader = response.Headers[HeaderNames.SetCookie].Where(s => !s.Contains(cookieName)).Aggregate(new StringValues(), (current, s) => StringValues.Concat(current, s));
response.Headers[HeaderNames.SetCookie] = cookieHeader;
}
return Task.CompletedTask;
}, context.Response);
return next(context);
});
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationScheme = CookieAuthenticationDefaults.AuthenticationScheme,
CookiePath = "/",
CookieName = cookieName,
AutomaticAuthenticate = true,
ExpireTimeSpan = TimeSpan.FromMinutes(20),
AutomaticChallenge = true,
SlidingExpiration = true,
CookieSecure = CookieSecurePolicy.Always
// Additional auth options...
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment