UPD: Same question asked on security.stackexchange.com and the answer I got is different. Please follow there, to get the correct answer!
I'm running a rather large site with thousands of visits every day, and a rather large userbase.
Since I started migrating to MVC 3, I've been putting the AntiForgeryToken in a number of forms, that modify protected data etc.
Some other forms, like the login / registration also use the AntiForgeryToken now, but I'm becoming dubious about their need there in the first place, for a couple reasons...
There are possibly other reasons why would one use/not use the token in their forms.. Am I correct in assuming that using the token in every post form is bad / overkill, and if so - what kind of forms would benefit from it, and which ones would definitely NOT benefit?
This ensures that a form being posted to the server was actually generated by the same server. Thus fake forms that do not have the AntiForgeryToken from the correct server, gets rejected. As you can see, after adding the AntiForgeryToken, if you click on the malicious link, you get the above error instead.
Using AntiForgeryToken helps mitigate against cross-site request forgery attacks. When you use it, your form will contain a hidden field and a corresponding cookie will also be set in the browser.
The ValidateAntiForgeryToken attribute requires a token for requests to the action methods it marks, including HTTP GET requests.
The form tag helper will automatically add the anti forgery token. (Unless you use it as a standard html form element, manually adding an action attribute). Check the source code of the form tag helper, you will see the following at the end of the Process method.
Anti forgery tokens are useless in public parts of the site where users are not yet authenticated such as login and register forms. The way CSRF attack works is the following:
So you could use anti forgery tokens on authenticated parts of your site containing actions that could modify somehow the user state.
Remark: checking the Referer header for identifying that a request came from your site is not secure. Anyone can forge a request and spoof this header.
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