簡體   English   中英

使用jQuery Ajax和Html.AntiForgeryToken()時,防偽表單字段“__RequestVerificationToken”不存在

[英]anti-forgery form field “__RequestVerificationToken” is not present when using jQuery Ajax and the Html.AntiForgeryToken()

我為這個問題的接受答案中描述的解決方案實現了Razor等價物: jQuery Ajax調用和Html.AntiForgeryToken()但是我一直得到以下異常:

System.Web.Mvc.HttpAntiForgeryException(0x80004005):不存在所需的防偽表單字段“__RequestVerificationToken”。

編輯

我這樣做是為了解決這個問題:

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

但不幸的是,FormData()僅在最新的瀏覽器版本中受支持。 在FormData()之前可以使用的任何解決方法?

編輯我想知道為什么ValidateAntiForgeryTokenAttribute只在Form數據中查找AntyForgeryToken,並且不會在潰敗值中查找它,如下面密碼類AntiForgeryTokenStoreAntiForgeryWorker的代碼中所示

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

好了,經過深入挖掘后,我在這個鏈接中找到了一個很好的解決方案: ASP.NET MVC Ajax CSRF保護使用jQuery 1.5

據我所知,在這個問題的選擇答案中描述的解決方案: jQuery Ajax調用和Html.AntiForgeryToken() ,應該不起作用(事實上它對我來說失敗了)。

創建antiforgerytoken:

@using (Html.BeginForm()) {
    @Html.AntiForgeryToken()
    <input ...>
}

創建一個函數以將令牌添加到ajax請求:

function addRequestVerificationToken(data) {
    data.__RequestVerificationToken=$('input[name=__RequestVerificationToken]').val();
    return data;
};

然后你可以像這樣使用它:

$.ajax({
   type: "POST",
   url: '@Url.Action("MyMethod", "MyController", new {area = "MyArea"})',
   dataType: "json",
   traditional: true,
   data: addRequestVerificationToken( { "id": "12345678" } );
})
.done(function(result) {
   if (result) {
       // Do something
   } else {
       // Log or show an error message
   }
   return false;
});

當你調用CallAjax()data來自何處? 我問,因為通常當您的數據來自表單時,您的CSRF令牌已經是表單的一部分,通常是在隱藏字段中。

<form action="yourPage.html" method="POST">
    <input type="hidden" name="__RequestVerificationToken" value="......"/>
    .... other form fields ....
</form>

因此,如果您的數據全部來自表單,那么您應該確保令牌是該表單的隱藏部分,並且應該自動包含令牌。

如果您的數據來自表單以外的某個地方,那么您可以將您的令牌存儲在某處,然后在數據匯編后包含它是可以理解的。 但您可以考慮將令牌添加到數據中,而不是從令牌構建新對象,然后將所有數據添加到其中。

if (type == 'POST') {
    data._RequestVerificationToken = $("input[name='__RequestVerificationToken']").val();
    ajaxOptions.processData = false;
    ajaxOptions.contentType = false;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM