I make my EDIT view a dialog box and i need to pass AntiForgeryToken with it. But I've tried several times and still I can't pass it.
$("#EditSave").click(function(e) {
e.preventDefault();
$.ajax({
url: 'PIS/Edit',
type: "POST",
dataType: 'json',
data: $('form').serialize(),
success: function (data) {
if (data.Save) {
dialog.dialog('close');
var notification = {
type: "success",
title: "Successfully",
message: "save ",
icon: 'glyphicon glyphicon-ok-sign'
};
showNotification(notification);
updatePartial();
}
if (data.AlreadyExist) {
var notification = {
type: "danger",
title: "Warning!",
message: "Already Exist",
icon: 'glyphicon glyphicon-exclamation-sign'
};
showNotification(notification);
}
if (data.Back) {
var notification = {
type: "danger",
title: "Warning!",
message: "Information was not successfully saved. Please check required fields.",
icon: 'glyphicon glyphicon-exclamation-sign'
};
showNotification(notification);
}
else {
debugger
dialog.dialog('close');
}
},
error: function (xhr, status, error) {
alert(xhr.responseText);
}
})
return false;
})
This is my controller action and I need to pass the token on my view so that cross-side scripting can be prevented because this is an Edit view in a form of a dialog box.
public async Task<PartialViewResult> Edit(string id)
{
return PartialView();
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<JsonResult>Edit(PISEditViewModel viewModel)
{
return Json(new {Back = true });
}
This is the error message i got when passing $('form').serialize()
The anti-forgery token could not be decrypted. If this application is hosted by a Web Farm or cluster, ensure that all machines are running the same version of ASP.NET Web Pages and that the <machineKey> configuration specifies explicit encryption and validation keys. AutoGenerate cannot be used in a cluster.