ajax400 not present,anti-forgery form field “__RequestVerificationToken” is not present when using j...

I implemented the Razor equivalent for the solution described in the accepted answer for this Question:

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);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值