Prevent Cross-Site Request Forgery (CSRF) using ASP.NET MVC’s AntiForgeryToken() helper
ASP.NET, MVC, Security September 1st, 2008Update: Since the Release Candidate of ASP.NET MVC, these anti-forgery helpers have been promoted to be included in the core ASP.NET MVC package (and not in the Futures assembly).
Cross-site scripting (XSS) is widely regarded as the number one security issue on the web. But since XSS gets all the limelight, few developers pay much attention to another form of attack that’s equally destructive and potentially far easier to exploit. Your application can be vulnerable to cross-site request forgery (CSRF) attacks not because you the developer did something wrong (as in, failing to encode outputs leads to XSS), but simply because of how the whole Web is designed to work. Scary!
How CSRF works
So, what’s it all about? All web application platforms are potentially vulnerable to CSRF, but in this post I’ll focus on ASP.NET MVC. Imagine you have a controller class as follows:
public class UserProfileController : Controller { public ViewResult Edit() { return View(); } public ViewResult SubmitUpdate() { // Get the user's existing profile data (implementation omitted) ProfileData profile = GetLoggedInUserProfile(); // Update the user object profile.EmailAddress = Request.Form["email"]; profile.FavoriteHobby = Request.Form["hobby"]; SaveUserProfile(profile); ViewData["message"] = "Your profile was updated."; return View(); } }
This is all very normal. First, the visitor goes to Edit(), which renders some form to let them change their user profile details. Secondly, they post that form to SubmitUpdate(), which saves the changes to their profile record in the database. There’s no XSS vulnerability here. Everything’s fine, right? We implement this sort of thing all the time…
Unfortunately, this innocent controller is an easy target for CSRF. Imagine that an attacker sets up the following HTML page and hosts it on some server of their own:
<body onload="document.getElementById('fm1').submit()">
<form id="fm1" action="http://yoursite/UserProfile/SubmitUpdate" method="post">
<input name="email" value="hacker@somewhere.evil" />
<input name="hobby" value="Defacing websites" />
</form>
</body>Next, they somehow persuade a victim to visit this page (basic social engineering, look it up). When this HTML page loads, it submits a valid form post to /UserProfile/SubmitUpdate on your server.
Assuming you’re using Windows authentication or some kind of cookie-based authentication system such as Forms Authentication, the automated form post will be processed within the victim’s established authentication context, and will successfully update the victim’s email address to something under the attacker’s control. All the attacker has to do now is use your “forgotten password” facility, and they’re taken control of the victim’s account.
Of course, instead of changing an victim’s email address, they can perform any action that the victim can perform with a single POST request. For example, they might be able to grant administrative permissions to another account, or post something defamatory to a CMS.
Ways to stop CSRF
There are two main ways to block CSRF:
- Check that incoming requests have a Referer header referencing your domain. This will stop requests unwittingly submitted from a third-party domain. However, some people disable their browser’s Referer header for privacy reasons, and attackers can sometimes spoof that header if the victim has certain versions of Adobe Flash installed. This is a weak solution.
- Put a user-specific token as a hidden field in legitimate forms, and check that the right value was submitted. If, for example, this token is the user’s password, then a third-party can’t forge a valid form post, because they don’t know each user’s password. However, don’t expose the user’s password this way: Instead, it’s better to use some random value (such as a GUID) which you’ve stored in the visitor’s Session collection or into a Cookie.
Using the AntiForgeryToken helpers
With Preview 5, Microsoft has added a set of helpers to the “futures” assembly, Microsoft.Web.Mvc.dll,The core ASP.NET MVC package includes a set of helpers that give you a means to detect and block CSRF using the “user-specific tokens” technique.
To use these helpers to protect a particular form, put an Html.AntiForgeryToken() into the form, e.g.,
<% using(Html.Form("UserProfile", "SubmitUpdate")) { %> <%= Html.AntiForgeryToken() %> <!-- rest of form goes here --> <% } %>
This will output something like the following:
<form action="/UserProfile/SubmitUpdate" method="post">
<input name="__RequestVerificationToken" type="hidden" value="saTFWpkKN0BYazFtN6c4YbZAmsEwG0srqlUqqloi/fVgeV2ciIFVmelvzwRZpArs" />
<!-- rest of form goes here -->
</form>At the same time, Html.AntiForgeryToken() will give the visitor a cookie called __RequestVerificationToken, with the same value as the random hidden value shown above.
Next, to validate an incoming form post, add the [ValidateAntiForgeryToken] filter to your target action method. For example,
[ValidateAntiForgeryToken] public ViewResult SubmitUpdate() { // ... etc }
This is an authorization filter that checks that:
- The incoming request has a cookie called __RequestVerificationToken
- The incoming request has a Request.Form entry called __RequestVerificationToken
- These cookie and Request.Form values match
Assuming all is well, the request goes through as normal. But if not, boom!, there’s an authorization failure with message “A required anti-forgery token was not supplied or was invalid”.
This prevents CSRF because even if a potential victim has an __RequestVerificationToken cookie, an attacker can’t find out its value, so they can’t forge a valid form post with the same value in Request.Form. But legitimate users aren’t inconvenienced at all; the mechanism is totally silent.
Using salt
Salt? What? In case you want to protect multiple forms in your application independently of each other, you can use a “salt” value when you call Html.AntiForgeryToken(), e.g.,
<%= Html.AntiForgeryToken("someArbitraryString") %>
… and also in [ValidateAntiForgeryToken], e.g.,
[ValidateAntiForgeryToken(Salt="someArbitraryString")] public ViewResult SubmitUpdate() { // ... etc }
Salt is just an arbitrary string. A different salt value means a different anti-forgery token will be generated. This means that even if an attacker manages to get hold of a valid token somehow, they can’t reuse it in other parts of the application where a different salt value is required. (If anyone can suggest other use cases for salt, please let me know.)
Limitations of the Anti-Forgery helpers
ASP.NET MVC’s anti-CSRF helpers work very nicely, but you should be aware of a few limitations:
- All legitimate visitors must accept cookies (otherwise, [ValidateAntiForgeryToken] will deny their form posts). Arguably this isn’t a limitation, because unless visitors allow cookies, you probably don’t have anything to protect anyway.
- It only works with POST requests, not GET requests. Arguably this isn’t a limitation, because under the normal HTTP conventions, you shouldn’t be using GET requests for anything other than read-only operations.
- It’s easily bypassed if you have any XSS holes on your domain. An XSS hole would allow an attacker to read a victim’s anti-forgery token value, then use it to forge valid posts. So, don’t have XSS holes!
- It relies on the potential victim’s browser implementing cross-domain boundaries solidly. Browsers are supposed to stop foreign domains from reading your app’s response text and cookies, and are supposed to stop foreign domains from writing cookies to your domain. If an attacker manages to find a way around this, they can bypass [ValidateAntiForgeryToken]. Of course that’s not supposed to be possible. For the most part, modern browsers block this line of attack.
In conclusion, ASP.NET MVC’s anti-CSRF helpers are easy to use, and work very nicely thank you!


September 2nd, 2008 at 6:11 pm
Great analysis, especially your section on Limitations. We are keeping this in the Futures because we haven’t run extensive security analysis on our approach. So we provide no guarantees that it will stop all CSRF attacks. But at the very least, we think it will help mitigate the risk and we hope to vet this with the community before rolling it into core.
September 2nd, 2008 at 6:48 pm
I’m going to assume that __MVC_AntiForgeryToken cookie has HttpOnly set. Otherwise, what’s stopping that javascript from reading __MVC_AntiForgeryToken and filling the form before calling submit()?
September 2nd, 2008 at 6:50 pm
Duh, that’s why the XSS disclaimer.
It’s probably already an HttpOnly anyways.
September 2nd, 2008 at 8:34 pm
@Haacked: Thanks - the technique you’ve applied is very similar to that I’ve seen tried and tested in past projects, so I can’t imagine there being any fundamental problems with it. Looks solid.
@Chris: Actually, when I tried it, I was surprised to see that it *doesn’t* set the HttpOnly flag. But thinking about it more, this doesn’t actually affect the fact that XSS can bypass it. If there is an XSS hole in your site, an attacker can read one of your forms across domains, extract out the token, and use it when submitting that form back across domains. The cookie (and whether it is HttpOnly or not) is irrelevant to the attacker.
September 3rd, 2008 at 7:04 am
[…] Prevent Cross-Site Request Forgery (CSRF) using ASP.NET MVC’s AntiForgeryToken() helper - Steve Sanderson shows how you can protect your ASP.NET MVC applications from Cross Site Scripting (XSS) exploits using the AntiForgeryToken helpers included in ASP.NET MVC PR5 […]
September 9th, 2008 at 1:47 pm
Why not save a copy of that token in the session? I’d guess that would be a safer way (to protect against malfunctioning browsers) because the client wouldn’t have that cookie, wouldn’t it?
Also, is it possible to validate the token by code? That way you could provide a per-user salt value, e.g. a username or password hash, which is not possible to do in attributes.
September 9th, 2008 at 2:51 pm
@Configurator - I guess they chose to use cookie storage rather than session storage for scalability (in web farm scenarios, you sometimes can’t use session storage). Also, session state is volatile (can be erased at any moment) whereas cookies can stick around indefinitely.
> Also, is it possible to validate the token by code?
Kind of, though it is awkward. You can instantiate a ValidateAntiForgeryTokenAttribute, passing a salt parameter to its constructor, then call its OnAuthorization() method. That gives you programmatic control over salt, rather than having to embed it in an attribute. It’s a bit awkward to do this because the API wasn’t designed for it - you’re expected to validate the token only using an attribute.
But out of interest, why do you want to do this? The token contains a user-specific random value anyway, so the token is always user-specific. What extra benefit do you expect to get from having a user-specific salt value?
September 9th, 2008 at 3:47 pm
I’m not sure I “get it” yet. If you’re using Forms based auth (dropping a cooking on their machine), and have a filter that requires authentication of the user before they can modify their account, does this still leave you open to CSRF? (This still assumes you are preventing XSS attacks)
Isn’t the anti-forgery token just yet-another way of authenticating the user, just like their Forms Auth token? How does the combination improve the situation?
September 9th, 2008 at 4:17 pm
> If you’re using Forms based auth (dropping a cooking on their machine), and have a filter
> that requires authentication of the user before they can modify their account,
> does this still leave you open to CSRF?
Depending on what other things your application lets that user do, yes, you can still be vulnerable to CSRF. It’s not about authenticating the user - it’s about authenticating that requests made by that user were really intentional and not automated by a third party. Wikipedia has a decent explanation of CSRF if you need to know more.
September 25th, 2008 at 5:25 pm
[…] Steve Sanderson’s blog » Blog Archive » Prevent Cross-Site Request Forgery (CSRF) using ASP.NET … […]
October 7th, 2008 at 5:56 pm
I don’t know how the token actually is calculated, just a Guid as in the post mentioned doesn’t seem very strong. Whta’s about calculating the token over different parts of information, making it more unique. Maybe take a combination of these:
- User specific value as now, maybe a Guid
- User’s IP address
- User’S Browser Agent string (I guess nobody will change it between a GET and corresponding POST)
December 8th, 2008 at 9:11 am
@Configurator: How would you identify the session? By yet another cookie? And then how would you prove that an authorized user had used that cookie to lookup the session and perform authorized-only actions? Your ’solution’ is a chicken and egg problem
December 16th, 2008 at 9:42 pm
[…] Dorrans created a filter for CSRF protection in ASP.NET. It’s inspired by the ASP.NET MVC CSRF token approach. It’s a simple and effective protection mechanism when you can’t use the ViewStateUserKey […]
January 7th, 2009 at 11:12 pm
I implemented FxCop rule to spot this now.
https://blogs.msdn.com/sfaust/archive/2009/01/07/fxcop-rule-to-verify-the-use-of-asp-net-mvc-antiforgerytokenattribute.aspx
January 28th, 2009 at 2:22 pm
[…] in from the futures assembly, the Anti Forgery Helper. It’s well worth taking a look at and Steve Sanderson did a great post on it while it was still in the Futures assembly (incidentally, his book, even in […]
February 8th, 2009 at 11:25 am
I trying to use it but I have exception with DummyUrl. App is working on IIS7 and Vista. :/
February 9th, 2009 at 10:55 am
@dario-g - your issue is described at http://www.codeplex.com/aspnet/WorkItem/View.aspx?WorkItemId=3005. The solution is to upgrade to the latest version of ASP.NET MVC (currently, the Release Candidate).
February 10th, 2009 at 7:47 pm
This is great!!
This is exactly what I was looking for.
Thank You!!
February 11th, 2009 at 12:33 pm
I am not able to implment in my site. I am using ASP.net and C# as code behind.
I am puuting
in my aspx page , but I am confuse were should we put
[ValidateAntiForgeryToken]
public ViewResult SubmitUpdate()
{
// … etc
}
in Whicjh file should we put above code?
Thanks
APS
February 12th, 2009 at 7:53 am
@Ajit - this post is about ASP.NET MVC. You can’t use [ValidateAntiForgeryToken] with traditional ASP.NET WebForms.
March 12th, 2009 at 9:25 pm
Regarding “salt”, the biggest benefit I see is that it provides protection against forms of dictionary attack. If an attacker can examine enough encrypted but unsalted bytes, it is more likely for them to spot a pattern and crack the code. With unique salt sprinkled in as part of the encryption, it makes duplication and/or patterns in the encrypted data harder to recognize. I think extra salt is a bit overkill though if the value you’re encrypting is itself just a random nonce used as a temporary security token.
May 28th, 2009 at 2:54 pm
You write:”This prevents CSRF because even if a potential victim has an __RequestVerificationToken cookie, an attacker can’t find out its value, so they can’t forge a valid form post with the same value in Request.Form.”
But it seems not true. Why I can’t find out its value?
I can simply do that:
__RequestVerificationToken.Value=Request.Cookies[cookiename].Value;
Where cookiename is “__RequestVerificationToken_” + encoded AppPath.
Now I have token value and can do valid post.
May 28th, 2009 at 6:06 pm
@Roman - this mechanism protects against *request spoofing*, nothing else. The attack you describe would require an attacker to be able to run arbitrary .NET code on your server, which you should never allow, and if you do you have far worse security problems to worry about.
May 29th, 2009 at 11:16 am
Ahh, sure! Attacker can’t read cookie from another domain. Ok, thanx!
June 10th, 2009 at 1:52 pm
Ну и после такого, как говорится, хотелось бы заслушать начальника транспортного цеха
June 29th, 2009 at 11:27 pm
How is the request verification token user specific?
For instance, if the token is based on the IP address, that might be a problem for users who use proxy servers for anonymous surfing.
Ideally, you want it to be based on the username in the membership provider, so the token will always work.
June 30th, 2009 at 6:35 am
@mmtache - it’s a random value (like a GUID), stored in the user’s cookies, so unique to that user.
July 1st, 2009 at 4:10 am
А комментарии тут действительно интересные. Буду следить за комментами и дальше