Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is .AspNetCore.Antiforgery.xxxxxxx cookie in .Net Core?

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?

like image 219
Sharif Mamun Avatar asked Sep 13 '17 23:09

Sharif Mamun


People also ask

What is ASP.NET Core Antiforgery 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.

What is the Antiforgery token used for?

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.

What is cookies in .NET core?

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.

What is __ Requestverificationtoken cookie?

__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.


1 Answers

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

  • instead of cookie the header is used to pass token
  • cookie with token has the different name than the ASP.NET Core expected.

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:

  • Preventing Cross-Site Request Forgery (XSRF/CSRF) Attacks in ASP.NET Core
  • readme in Antiforgery repo contains links to samples
  • SO: Using the antiforgery cookie in ASP.NET Core but with a non-default CookieName
like image 107
Set Avatar answered Oct 11 '22 06:10

Set