防伪表单字段" __ RequestVerificationToken"是使用jQuery的Ajax和Html.AntiForgeryToken不用时present() [英] anti-forgery form field "__RequestVerificationToken" is not present when using jQuery Ajax and the Html.AntiForgeryToken()
问题描述
我实现了剃须刀等同于接受的答案对于这个问题描述的解决方案: jQuery Ajax调用和Html.AntiForgeryToken() 的
但我一直得到以下异常:
System.Web.Mvc.HttpAntiForgeryException(0X80004005):所需
防伪型窗体域__RequestVerificationToken不是present。
块引用>的修改的
我manged要解决它这样做的:
函数AddAntiForgeryToken(数据){
data.append('__ RequestVerificationToken',$('#__ AjaxAntiForgeryForm输入[名称= __ RequestVerificationToken]')VAL());
返回的数据;
};功能CallAjax(URL,类型,数据,成功,错误){ VAR ajaxOptions = {URL:键入:类型,则contentType:应用/ JSON'}; 如果(类型=='POST'){
变种FD =新window.FormData();
FD = AddAntiForgeryToken(FD);
$。每个(数据,功能(I,N){
fd.append(I,N);
});
数据= FD;
ajaxOptions.processData = FALSE;
ajaxOptions.contentType = FALSE;
} ajaxOptions.data =数据; 如果(成功)ajaxOptions.success =成功; //如果有自定义错误处理废了一般状态code设置。
如果(错误){
ajaxOptions.error =错误;
ajaxOptions.status code = NULL;
}; $阿贾克斯(ajaxOptions);
}但不幸的是FORMDATA()在最新的浏览器版本仅支持。
任何解决方法,因为引入了可能FORMDATA()前工作的?的修改的
我不知道为什么的 ValidateAntiForgeryTokenAttribute 的只在表格数据查找AntyForgeryToken,并没有寻找它的溃败值可以在密封类的code见下面的 AntiForgeryTokenStore 的和的 AntiForgeryWorker 的?公共void验证(HttpContextBase的HttpContext)
{
this.CheckSSLConfig(HttpContext的);
AntiForgeryToken cookieToken = this._tokenStore.GetCookieToken(HttpContext的);
AntiForgeryToken formToken = this._tokenStore.GetFormToken(HttpContext的);
this._validator.ValidateTokens(HttpContext的,AntiForgeryWorker.ExtractIdentity(HttpContext的),cookieToken,formToken);
}
公共AntiForgeryToken GetFormToken(HttpContextBase的HttpContext)
{
字符串serializedToken = httpContext.Request.Form [this._config.FormFieldName]
如果(string.IsNullOrEmpty(serializedToken))
返回(AntiForgeryToken)空;
其他
返回this._serializer.Deserialize(serializedToken);
}
解决方案好了,挖多一些我找到了一个很好的解决方案,以我在这个环节出现问题后:
据我了解在这个问题的答案选择描述的解决方案
ASP.NET MVC阿贾克斯CSRF保护使用jQuery 1.5:<一href=\"http://stackoverflow.com/questions/4074199/jquery-ajax-calls-and-the-html-antiforgerytoken\">jQuery Ajax调用和Html.AntiForgeryToken(),不应该工作(实际上它失败对我来说)。
I implemented the Razor equivalent for the solution described in the accepted answer for this Question: jQuery Ajax calls and the Html.AntiForgeryToken() But I kept getting the following exception:
System.Web.Mvc.HttpAntiForgeryException (0x80004005): The required anti-forgery form field "__RequestVerificationToken" is not present.
edit
I manged to workaround it doing this:
function AddAntiForgeryToken(data) { data.append('__RequestVerificationToken',$('#__AjaxAntiForgeryForm input[name=__RequestVerificationToken]').val()); return data; }; function CallAjax(url, type, data, success, error) { var ajaxOptions = { url: url, type: type, contentType: 'application/json'}; if (type == 'POST') { var fd = new window.FormData(); fd = AddAntiForgeryToken(fd); $.each(data, function (i, n) { fd.append(i,n); }); data = fd; ajaxOptions.processData = false; ajaxOptions.contentType = false; } ajaxOptions.data = data; if (success) ajaxOptions.success = success; //If there is a custom error handler nullify the general statusCode setting. if (error) { ajaxOptions.error = error; ajaxOptions.statusCode = null; }; $.ajax(ajaxOptions); }
But unfortunately FormData() is only supported in latest browser versions. Any workaround that could work before FormData() as introduced?
edit I wonder why the ValidateAntiForgeryTokenAttribute looks for the AntyForgeryToken only in the Form data, and doesn't look for it in the rout values as you can see below in the code of the sealed classes AntiForgeryTokenStore and AntiForgeryWorker?
public void Validate(HttpContextBase httpContext) { this.CheckSSLConfig(httpContext); AntiForgeryToken cookieToken = this._tokenStore.GetCookieToken(httpContext); AntiForgeryToken formToken = this._tokenStore.GetFormToken(httpContext); this._validator.ValidateTokens(httpContext, AntiForgeryWorker.ExtractIdentity(httpContext), cookieToken, formToken); } public AntiForgeryToken GetFormToken(HttpContextBase httpContext) { string serializedToken = httpContext.Request.Form[this._config.FormFieldName]; if (string.IsNullOrEmpty(serializedToken)) return (AntiForgeryToken) null; else return this._serializer.Deserialize(serializedToken); }
解决方案Well, after digging some more I found a nice solution to my problem in this link: ASP.NET MVC Ajax CSRF Protection With jQuery 1.5
As far as I understand the solution described in the chosen answer for this question: jQuery Ajax calls and the Html.AntiForgeryToken(), shouldn't work (indeed it failed for me).
这篇关于防伪表单字段&QUOT; __ RequestVerificationToken&QUOT;是使用jQuery的Ajax和Html.AntiForgeryToken不用时present()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!