I thinks you can use AntiForgeryToken
,
In your view you need the token so that it is available to JavaScript.Add following line the above javascript just use the common HTML-Helper.
@Html.AntiForgeryToken()
Append it to your ajax request so that you don't have to repeat yourself
$(document).ready(function () {
var securityToken = $('[name=__RequestVerificationToken]').val();
$('body').bind('ajaxSend', function (elm, xhr, s) {
if (s.type == 'POST' && typeof securityToken != 'undefined') {
if (s.data.length > 0) {
s.data += "&__RequestVerificationToken=" + encodeURIComponent(securityToken);
}
else {
s.data = "__RequestVerificationToken=" + encodeURIComponent(securityToken);
}
}
});
});
And in you controller simply add standard ASP.Net MVC Anti-CSRF mechanism. like
[HttpPost]
[Authorize]
[ValidateAntiForgeryToken]
public JsonResult YourMethod(string param)
{
// do whatever
return Json(true);
}