ValidateAntiForgery

I am trying to send the __RequestVerificationToken so I can use [ValidateAntiForgery] in my controller when editing a grid inline. I have tried the answer here but that doesn't seem to work for me I still get "The required anti-forgery form field __RequestVerificationToken is not present" . Any suggestions?

3 Replies

IR Isuriya Rajan Syncfusion Team April 12, 2018 07:31 PM UTC

Hi Aaron,  
 
For your requirement we have created the sample and attached for your reference. 
 
 
ValidateAntiForgeryToken attribute is expecting the post data to be in FormData format instead of JSON. Hence in this sample, we have extended the UrlAdaptor to pass the AntiForgery token to server side with contentType as application/x-www-form-urlencoded; charset=UTF-8.  
 
Refer the below code example:  
 
window.customAdaptor = new ej.data.UrlAdaptor(); 
    customAdaptor = ej.base.extend(customAdaptor, { 
 
          processResponse: function (data, ds, query, xhr, request, changes) { 
              request.data = JSON.stringify(data); 
              return ej.data.UrlAdaptor.prototype.processResponse.call(this,data, ds, query, xhr, request, changes) 
        }, 
        insert: function (dm, data, tableName) { 
            return { 
                url: dm.dataSource.insertUrl || dm.dataSource.crudUrl || dm.dataSource.url, 
                data: $.param({ 
                    __RequestVerificationToken: document.getElementsByName("__RequestVerificationToken")[0].value, 
                    value: data, 
                    table: tableName, 
                    action: 'insert' 
                }), 
                contentType: 'application/x-www-form-urlencoded; charset=UTF-8' 
            } 
        }, 
        update: function (dm, keyField, value, tableName) { 
            return { 
                    url: dm.dataSource.updateUrl || dm.dataSource.crudUrl || dm.dataSource.url, 
                data: $.param({ 
                    __RequestVerificationToken: document.getElementsByName("__RequestVerificationToken")[0].value, 
                    value: value, 
                    table: tableName, 
                    action: 'insert' 
                }), 
                contentType: 'application/x-www-form-urlencoded; charset=UTF-8' 
            }; 
        }, 
    });  
 
Also we have assigned the extended adaptor in grid load event as below. 
 
  function load(args) { 
        this.dataSource.adaptor = customAdaptor; 
         
    } 
 
 
Please get back to us , If you need any other assistance. 
 
Regards, 
Isuriya R 
 



AK Aaron Khan April 13, 2018 03:09 PM UTC

Thanks this solved my issue.


DR Dhivya Rajendran Syncfusion Team April 16, 2018 12:31 PM UTC

Hi Aaron, 
 
We are glad to hear that your issue has been resolved. Please get back to us if you need further assistance. 
 
Regards, 
R.Dhivya 


Loader.
Up arrow icon