-
19 Oct 2010 9:05 PM #1
How to pass the anti forgery token to an ASP.NET MVC action method?
How to pass the anti forgery token to an ASP.NET MVC action method?
Can anyone using ASP.NET MVC tell me how to pass the anti-forgery token back successfully to action methods decorated with the [ValidateAntiForgeryToken] attribute?
I got code like the below but can't figure out how to pass the token back properly in my JSON. Everything I've tried thus far results in an "A required anti-forgery token was not supplied or was invalid."
Code:
Thanks -wgCode:[Transaction] [ValidateAntiForgeryToken] [HttpDelete] public ActionResult Delete(int id) { ... } [Transaction] [ValidateAntiForgeryToken] [HttpPut] public ActionResult Edit(Location location) { ... } [Transaction] [ValidateAntiForgeryToken] [HttpPost] public ActionResult Create(Location location) { ... }
-
14 Jun 2011 5:02 AM #2
Ext.onReady( function() {
var myToken = document.getElementsByName('__RequestVerificationToken').item(0).value;
MyExtForm.MyHiddenField.setValue(myToken);
}
-
16 Jun 2011 10:48 AM #3
If you're using Ajax you could use either the Ext.Ajax.extraParams property or the beforerequest event.
-
6 Mar 2013 3:23 PM #4
You may also create a proxy that will set AFT header at run-time, before the request is being sent. That will allow you to use config and programmatic proxies. It works with Sencha Touch and MVC4 WebAPI.
Code:Ext.define('WApp.model.AftRestProxy', { extend: 'Ext.data.proxy.Rest', requires: [ 'Ext.data.proxy.Rest' ], alias: 'proxy.aftrest', constructor: function (config) { var defaults = { timeout: 10000 }; this.callParent([Ext.Object.merge(defaults, config)]); }, buildRequest: function (operation) { // Add AFT header to proxy's headers var r = this.callParent([operation]); var afth = { 'RequestVerificationToken': Ext.get("antiForgeryToken").getValue() }; var p = r.getProxy(); if (p) { var h = Ext.Object.merge(p.getHeaders() || {}, afth); p.setHeaders(h); } return r; }});Code:Ext.define('WApp.model.UserProfile', { extend: 'Ext.data.Model', requires: [ 'WApp.model.AftRestProxy' ], config: { fields: [ ], proxy: { type: 'aftrest', url: '/api/auth/getuserprofile' } }});
Similar Threads
-
Update method for Editor Grid in C# (ASP.NET MVC)
By Grisco in forum Ext 3.x: Help & DiscussionReplies: 3Last Post: 3 Nov 2010, 11:33 PM -
ExtJS and ASP.NET MVC
By wgpubs in forum Ext 2.x: Help & DiscussionReplies: 0Last Post: 5 Dec 2008, 5:41 PM -
Problem combining extjs 2.0 with asp.net/ asp.net mvc/html
By ritcoder in forum Ext 2.x: Help & DiscussionReplies: 1Last Post: 12 Jul 2008, 3:58 PM -
MVC for asp.net/VS 2008
By DragonFist in forum Community DiscussionReplies: 15Last Post: 16 Jun 2008, 7:47 AM -
ASP.Net MVC Extjs support - MVC Javascript team looking for ExtJS experience
By DragonFist in forum Community DiscussionReplies: 2Last Post: 4 Feb 2008, 2:05 PM


Reply With Quote