I am using Membership.create user function, then the following error is occurring,
The required anti-forgery form field \"__RequestVerifi
In my case I was getting this error while making an AJAX post, it turned out to be that the __RequestVerificationToken value wasn't being passed across in the call. I had to manually find the value of this field and set this as a property on the data object that's sent to the endpoint.
i.e.
data.__RequestVerificationToken = $('input[name="__RequestVerificationToken"]').val();
HTML
Javascript
$(document).on('click', '#myForm .submitButton', function () {
var myData = { ... };
myData.__RequestVerificationToken = $('#myForm input[name="__RequestVerificationToken"]').val();
$.ajax({
type: 'POST',
url: myUrl,
data: myData,
contentType: 'application/x-www-form-urlencoded; charset=utf-8',
dataType: 'json',
success: function (response) {
alert('Form submitted');
},
error: function (e) {
console.error('Error submitting form', e);
alert('Error submitting form');
},
});
return false; //prevent form reload
});
Controller
[HttpPost]
[Route("myUrl")]
[ValidateAntiForgeryToken]
public async Task MyUrlAsync(MyDto dto)
{
...
}