I was trying to use ValidateAntiForgeryToken in .Net Core but I was getting .AspNetCore.Antiforgery.xxxxxxx cookie is missing.
What is this .AspNetCore.Antiforgery.xxxxxxx cookie?
Cross-site request forgery (also known as XSRF or CSRF) is an common attack against web apps that store authentication tokens in the cookies. Browser will automatically attach these authentication cookies with every request to the website.
To help prevent CSRF attacks, ASP.NET MVC uses anti-forgery tokens, also called request verification tokens. The client requests an HTML page that contains a form. The server includes two tokens in the response. One token is sent as a cookie.
Take advantage of cookies to store and retrieve user-specific information in your ASP.NET Core web application. Nastco/Thinkstock. A cookie is a piece of data typically used to store information about the user and is stored on the user's computer.
__RequestVerificationToken Session www.ese-hormones.org Strictly Necessary This is an anti-forgery cookie set by web applications built using ASP.NET MVC technologies. It is designed to stop unauthorized posting of content to the website, known as Cross-Site Request Forgery.
ASP.NET Core looks for this cookie to find the X-CSRF token.
The
ValidateAntiForgeryToken
is an action filter that can be applied to an individual action, a controller, or globally for the app. Requests made to actions that have this filter applied will be blocked unless the request includes a valid antiforgery token.
In general ASP.NET Core may look for the token in cookie or header. So you may have the situation when
By default, the ASP.NET Core will generate and expect a unique cookie name beginning with the DefaultCookiePrefix (".AspNetCore.Antiforgery.").
This could be overriden using an antiforgery option CookieName
:
services.AddAntiforgery(options => options.CookieName = "X-CSRF-TOKEN-COOKIENAME");
For .Net Core 2.0.0 or greater there will be changes:
Reference: https://docs.microsoft.com/en-us/dotnet/api/Microsoft.AspNetCore.Antiforgery.AntiforgeryOptions?view=aspnetcore-2.0
For that use following:
services.AddAntiforgery(options => options.Cookie.Name = "X-CSRF-TOKEN-COOKIENAME");
If talking about header, name could be specified by:
services.AddAntiforgery(options => options.HeaderName = "X-XSRF-TOKEN");
Look into:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With