I am using Membership.create
user function, then the following error is occurring,
The required anti-forgery form field \"__RequestVerifi
Make sure in your controller that you have your http attribute like:
[HttpPost]
also add the attribute in the controller:
[ValidateAntiForgeryToken]
In your form on your view you have to write:
@Html.AntiForgeryToken();
I had Html.AntiForgeryToken(); without the @ sign while it was in a code block, it didn't give an error in Razor but did at runtime. Make sure you look at the @ sign of @Html.Ant.. if it is missing or not
In my case it was due to adding requireSSL=true
to httpcookies
in webconfig which made the AntiForgeryToken stop working. Example:
<system.web>
<httpCookies httpOnlyCookies="true" requireSSL="true"/>
</system.web>
To make both requireSSL=true
and @Html.AntiForgeryToken()
work I added this line inside the Application_BeginRequest
in Global.asax
protected void Application_BeginRequest(object sender, EventArgs e)
{
AntiForgeryConfig.RequireSsl = HttpContext.Current.Request.IsSecureConnection;
}
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)
{
....
In my case incorrect domain in web.config for cookies was the reason:
<httpCookies domain=".wrong.domain.com" />
Because this comes up with the first search of this:
I had this issue only in Internet Explorer and couldnt figure out the what the issue was. Long story short it was not saving the cookie portion of the Token because our (sub)domain had an underscore in it. Worked in Chrome but IE/Edge didnt not like it.
You have [ValidateAntiForgeryToken]
attribute before your action. You also should add @Html.AntiForgeryToken()
in your form.