I am currently developing an MVC application in ASP.net. I am using AJAX.ActionLink to provide a delete link in a list of records, however this is very insecure. I have put
You can use AntiForgeryToken with Ajax.ActionLink but you need to manually insert the AntiForgeryToken into the header of your request like so:
function GetAntiForgeryToken(){
var tokenWindow = window;
var tokenName = "__RequestVerificationToken";
var tokenField = $(tokenWindow.document).find("input[type='hidden'][name='" + tokenName + "']");
if (tokenField.length == 0) {return null;}
else {
return {
name: tokenName,
value: tokenField.val()
};
}
};
Then, we can use $.ajaxPrefilter to insert it into the header:
$.ajaxPrefilter(
function (options, localOptions, jqXHR) {
var token = GetAntiForgeryToken();
jqXHR.setRequestHeader(token.name, token.value);
}
);
I wrote a post about it here. Hope this helps!