Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AutoValidateAntiForgeryToken vs. ValidateAntiForgeryToken

I was trying to secure a post method against side scripting just now through providing an anti forgery token but noticed, in .Net Core there is another attribute named as AutoAntiForgeryToken. The XML comments, and the online search did not provide much info on this new attribute.

Any help and description of what the new attribute is, will be much appreciated.

like image 625
Arnold Zahrneinder Avatar asked Oct 09 '16 19:10

Arnold Zahrneinder


People also ask

What is Autovalidateantiforgerytoken?

An attribute that causes validation of antiforgery tokens for all unsafe HTTP methods. An antiforgery token is required for HTTP methods other than GET, HEAD, OPTIONS, and TRACE. It can be applied at as a global filter to trigger validation of antiforgery tokens by default for an application.

Do I need ValidateAntiForgeryToken?

Require antiforgery validationThe ValidateAntiForgeryToken attribute requires a token for requests to the action methods it marks, including HTTP GET requests. If the ValidateAntiForgeryToken attribute is applied across the app's controllers, it can be overridden with the IgnoreAntiforgeryToken attribute.

What is ValidateAntiForgeryToken in asp net core?

HttpPost: The HttpPost attribute which signifies that the method will accept Http Post requests. ValidateAntiForgeryToken: The ValidateAntiForgeryToken attribute is used to prevent cross-site request forgery attacks.

How do you use AutoValidateAntiforgeryTokenAttribute?

Remarks. AutoValidateAntiforgeryTokenAttribute can be applied as a global filter to trigger validation of antiforgery tokens by default for an application. Use IgnoreAntiforgeryTokenAttribute to suppress validation of the antiforgery token for a controller or action.


1 Answers

From AutoValidateAntiforgeryTokenAttribute documentation:

An attribute that causes validation of antiforgery tokens for all unsafe HTTP methods. An antiforgery token is required for HTTP methods other than GET, HEAD, OPTIONS, and TRACE. It can be applied at as a global filter to trigger validation of antiforgery tokens by default for an application.

AutoValidateAntiforgeryTokenAttribute allows to apply Anti-forgery token validation globally to all unsafe methods e.g. POST, PUT, PATCH and DELETE. Thus you don't need to add [ValidateAntiForgeryToken] attribute to each and every action that requires it.

To use it add the following code to your ConfigureServices method of Startup class

services.AddMvc(options => {     options.Filters.Add(new AutoValidateAntiforgeryTokenAttribute()); }); 

If you need to ignore Anti forgery validation you can add [IgnoreAntiforgeryToken] attribute to the action.

like image 93
Andrei Mihalciuc Avatar answered Oct 18 '22 20:10

Andrei Mihalciuc