Membership.create
用户函数,但出现以下错误,
我该如何解决?所需的防伪造表单字段“__RequestVerificationToken”不存在
Membership.create
用户函数,但出现以下错误,
我该如何解决?所需的防伪造表单字段“__RequestVerificationToken”不存在
Html.AntiForgeryToken();
,请将其更改为 @Html.AntiForgeryToken()
。在使用ASP.NET的个人用户帐户默认登录时,Chrome浏览器中出现了此错误。
.cshtml:
@using (Html.BeginForm("Login", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
@Html.AntiForgeryToken()
<h4>Use a local account to log in.</h4>
控制器:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
在我的情况下,web.config文件中的错误域名是cookie不起作用的原因:
<httpCookies domain=".wrong.domain.com" />
我是这样解决的
[AttributeUsage(AttributeTargets.Method)]
public class ExcludeFromAntiForgeryValidationAttribute : Attribute{
}
并将 System.Web.Helpers.AntiForgery.Validate(cookie != null ? cookie.Value : null, formToken)
放置在 if 条件中
bool shouldValidate =!filterContext.ActionDescriptor.GetCustomAttributes(typeof(ExcludeFromAntiForgeryValidationAttribute), true).Any();
if (shouldValidate){
System.Web.Helpers.AntiForgery.Validate(cookie != null ? cookie.Value : null, formToken);
}
__RequestVerificationToken
值。我不得不手动查找此字段的值,并将其设置为发送到端点的数据对象的属性。data.__RequestVerificationToken = $('input[name="__RequestVerificationToken"]').val();
HTML
<form id="myForm">
@Html.AntiForgeryToken()
<!-- other input fields -->
<input type="submit" class="submitButton" value="Submit" />
</form>
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
});
控制器
[HttpPost]
[Route("myUrl")]
[ValidateAntiForgeryToken]
public async Task<ActionResult> MyUrlAsync(MyDto dto)
{
...
}
public class MyDto { public bool Whatever { get; set; } }
。 - demoncodemonkey我想分享一下我的经验,我一直在使用ASP.NET MVC 4和AngularJS跟随防伪令牌教程,但每次使用$http.post请求时都会抛出异常,后来我发现解决方法就是在$http.post的头部添加'X-Requested-With': 'XMLHttpRequest',因为它似乎(filterContext.HttpContext.Request.IsAjaxRequest())
无法识别它是否为ajax请求,以下是我的示例代码。
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);
});
保存控制器
[HttpPost]
[MyValidateAntiForgeryToken]
public ActionResult User(UserModel usermodel)
{
....
因为这是第一次搜索出现的:
我只在Internet Explorer中遇到了这个问题,无法弄清楚问题所在。长话短说,它没有保存Token的cookie部分,因为我们的(子)域名中有一个下划线。在Chrome中工作正常,但IE/Edge不喜欢它。
<httpRuntime targetFramework="4.7.1" maxRequestLength="10096" />
ASP.NET 请求限制:http://msdn.microsoft.com/zh-cn/library/e1f13641.aspx IIS 请求限制:http://www.iis.net/ConfigReference/system.webServer/security/requestFiltering/requestLimits