i'd like to share mine, i have been following this anti forgerytoken tutorial
using asp.net mvc 4 with angularjs, but it throws an exception everytime i request using $http.post and i figured out the solution is just add
'X-Requested-With': 'XMLHttpRequest' to the headers of $http.post, because it seems like the (filterContext.HttpContext.Request.IsAjaxRequest())
does not recognize it as ajax and here is my example code.
App.js
var headers = {
'X-Requested-With': 'XMLHttpRequest',
'RequestVerificationToken': $scope.token,
'Content-Type': 'application/json; charset=utf-8;'
};
$http({
method: 'POST',
url: baseURL + 'Save/User',
data: JSON.stringify($scope.formData),
headers: headers
}).then(function (values) {
alert(values.data);
}).catch(function (err) {
console.log(err.data);
});
SaveController
[HttpPost]
[MyValidateAntiForgeryToken]
public ActionResult User(UserModel usermodel)
{
....