Asp.net mvc 仅Internet Explorer中的例外情况:“;“所需防伪表单字段”__RequestVerificationToken“;“不存在”;

Asp.net mvc 仅Internet Explorer中的例外情况:“;“所需防伪表单字段”__RequestVerificationToken“;“不存在”;,asp.net-mvc,internet-explorer,exception,csrf,antiforgerytoken,Asp.net Mvc,Internet Explorer,Exception,Csrf,Antiforgerytoken,我知道有很多类似的问题,但这是不同的: 此异常仅在IE中发生 这也是在登录页面。表单上有@Html.AntiForgeryToken(),使用或不使用此选项: var addAntiForgeryToken = function (data, form) { data.__RequestVerificationToken = $("#" + form + " input[type=hidden] [name='__RequestVerificationToken']").val();

我知道有很多类似的问题,但这是不同的: 此异常仅在IE中发生

这也是在登录页面。表单上有
@Html.AntiForgeryToken()
,使用或不使用此选项:

var addAntiForgeryToken = function (data, form) {
    data.__RequestVerificationToken = $("#" + form + " input[type=hidden]   [name='__RequestVerificationToken']").val();
return data;
};
var formdata = addAntiForgeryToken($(this), 'login-form').serialize(), 
\uu RequestVerificationToken
字段始终存在于formdata中(在IE的调试模式下);但ajax登录函数总是返回此异常。为什么?

The required anti-forgery form field "__RequestVerificationToken" is not present.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.Mvc.HttpAntiForgeryException: The required anti-forgery form field "__RequestVerificationToken" is not present.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[HttpAntiForgeryException (0x80004005): The required anti-forgery form field "__RequestVerificationToken" is not present.]
System.Web.Helpers.AntiXsrf.TokenValidator.ValidateTokens(HttpContextBase httpContext, IIdentity identity, AntiForgeryToken sessionToken, AntiForgeryToken fieldToken) +485
System.Web.Helpers.AntiXsrf.AntiForgeryWorker.Validate(HttpContextBase httpContext) +71
System.Web.Helpers.AntiForgery.Validate() +92
System.Web.Mvc.ValidateAntiForgeryTokenAttribute.OnAuthorization(AuthorizationContext filterContext) +18
System.Web.Mvc.ControllerActionInvoker.InvokeAuthorizationFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor) +97
System.Web.Mvc.Async.<>c__DisplayClass21.<begininvokeaction>
b__19(AsyncCallback asyncCallback, Object asyncState) +743
System.Web.Mvc.Async.WrappedAsyncResult`1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +14
System.Web.Mvc.Async.WrappedAsyncResultBase`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +128
System.Web.Mvc.Async.AsyncControllerActionInvoker.BeginInvokeAction(ControllerContext controllerContext, String actionName, AsyncCallback callback, Object state) +343
System.Web.Mvc.Controller.<beginexecutecore>
b__1c(AsyncCallback asyncCallback, Object asyncState, ExecuteCoreState innerState) +25
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +30
System.Web.Mvc.Async.WrappedAsyncResultBase`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +128
System.Web.Mvc.Controller.BeginExecuteCore(AsyncCallback callback, Object state) +465
System.Web.Mvc.Controller.<beginexecute>
b__14(AsyncCallback asyncCallback, Object callbackState, Controller controller) +18
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +20
System.Web.Mvc.Async.WrappedAsyncResultBase`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +128
System.Web.Mvc.Controller.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +374
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +16
System.Web.Mvc.MvcHandler.<beginprocessrequest>
b__4(AsyncCallback asyncCallback, Object asyncState, ProcessRequestState innerState) +52
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +30
System.Web.Mvc.Async.WrappedAsyncResultBase`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +128
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +384
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +48
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +16
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +301
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155
序列化数据如下所示:(但它表示它不存在)


你的剧本毫无意义。如果使用
var formdata=$('login-form').serialize()
然后该令牌将被序列化并包含在数据中(假设您在表单标记中包含了
@Html.AntiForgeryToken()
)。无需再次尝试添加。注意,我说过“无论是否使用
代码块
,\uu RequestVerificationToken字段始终存在于formdata中”,我的意思是这不是问题!在没有脚本的情况下使用
.serialize()
,效果很好!我发现我的问题是如何在IE中使用ajax函数。因为服务器端也不存在所有其他字段(当我禁用AntiForgeryToken属性时)。因此,我将添加我的ajax函数,看看它是否存在任何问题,即它在IE中不工作。我已经测试了
.serialize()
,但没有成功!
 $(function () {
        configValidations();
        $('#login-form').submit(function (e) {
            e.preventDefault();
            $("#result").html("");
            if ($(this).valid()) {
                var formdata = $('#login-form').serialize();
                wait();
                $.ajax({
                    url: this.action,
                    type: this.method,
                    cache: false,
                    data: formdata,
                    dataType: "json",                       
                    success: function (result) {
                        $('#result').html(ajaxSuccessMessage(""));
                    }
                }).done(function (response) {
                    unwait();
                    if (response.Content == null) {
                        $("#result").html(ajaxErrorMessage(response.Errors[0]));
                    }
                    else {
                        window.location = response.Content
                    }
                }).fail(function (error) {
                    $('#result').html(ajaxValidationBootstrap(error));
                }).always(function () {
                    unwait();
                });
            }
        });
    });
__RequestVerificationToken=BsLh3rc3zlgSna8CDaAN-sK7MsDL4ch0mjk8aW3eKtusSC1MYR4lAEo7HNJz3erAdi2Z3NvrXndFUoFw9FGL5SKTYp_HvFJI-TUg_neb9GQ1&ReturnUrl=&email=demo%40email.com&UserName=demouser&Password=demopasse