In our project, we’ve been using Phil Haack’s method for preventing cross-site request forgeries for JSON posts by inserting the request verification token as a header in the request, and then using a custom ValidateJsonAntiForgeryToken
attribute to validate it. And it’s been working just fine.
However, with the recent release of ASP.NET 4 MVC RC, it didn’t work anymore. To my initial dismay, it didn’t even compile anymore. Turns out that the method, AntiForgery.Validate(HttpContextBase httpContext, string salt)
, that hade been used to validate the tokens is now obsolete.
However, this turned out to be a good thing, as the MVC developers have made it easier to configure the anti-XSRF validation. You can now provide the tokens directly to the Validate method, and thus there is no need to create a wrapper for the HttpContext and the HttpRequest anymore.
Instead, you can just call the validate method with the proper tokens directly from your attribute:
1 | [ |
And, just to make this post complete, in case the original post is removed, this is the javascript required to add the header to the request:
1 | postJsonWithVerificationToken: function (options) { |
Finally, you just use it as you would the standard ValidateAntiForgeryToken
attribute, by decorating your action method like this:
1 | [ ] |
And in your form, you just call Html.AntiForgeryToken()
, just as you would for a normal form post.
A lot cleaner than the previous haack (although it was very clever)!