Adding AntiForgeryToken to MVC with jQuery

I’ve seen many posts on how to include MVC’s AntiForgeryToken on a view that is using a javascript framework for its UI. Well, below is a simple solution on how you can implement it. By the way, I am using MVC 3 and jQuery while writing this post.

First.
Include @Html.AntiForgeryToken() on your view. Below is an example view.

Second
On your controller, add the [ValidateAntiForgeryToken] attribute.

Finally
To complete the puzzle, lets post our data together with the antiforgery token to our controller action. You need to encode the value of __RequestVerificationToken before posting it, otherwise it will be useless.

And that’s it, as simple as the above.

Advertisements

3 thoughts on “Adding AntiForgeryToken to MVC with jQuery

Add yours

  1. hello!,I like your writing so much! share we keep up a correspondence extra about your article on AOL? I require an expert on this space to resolve my problem. May be that is you! Looking ahead to look you. ceffgdabdkdd

  2. Hi, everything is going well here and ofcourse every one is sharing information, that’s really fine, keep up writing. dgdkeeekeedk

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

Up ↑

%d bloggers like this: