Skip to content

Instantly share code, notes, and snippets.

@marcosdeaguiar
Created May 20, 2020 18:25
Show Gist options
  • Save marcosdeaguiar/946b216b81a7ed1a04716d0f474a200f to your computer and use it in GitHub Desktop.
Save marcosdeaguiar/946b216b81a7ed1a04716d0f474a200f to your computer and use it in GitHub Desktop.
public static class AntiforgeryCookieMiddleware
{
/// <summary>
/// Adds an antiforgery cookie to the response.
/// </summary>
/// <param name="app">Application builder instance.</param>
/// <param name="antiforgery">Antiforgery service.</param>
/// <param name="cookieName">Name of the cookie where the token will be added.</param>
public static void UseAntiforgeryCookieMiddleware(this IApplicationBuilder app,
IAntiforgery antiforgery,
string cookieName = "XSRF-TOKEN")
{
app.Use(async (context, next) =>
{
var tokens = antiforgery.GetAndStoreTokens(context);
context.Response.Cookies.Append(cookieName,
tokens.RequestToken,
new CookieOptions() { HttpOnly = false });
await next();
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment