第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

使用$ .ajax發(fā)布JSON數(shù)據(jù)時(shí),如何提供AntiForgeryToken?

使用$ .ajax發(fā)布JSON數(shù)據(jù)時(shí),如何提供AntiForgeryToken?

慕雪6442864 2019-09-21 15:35:39
我正在使用下面這篇文章的代碼:首先,我將使用控制器操作的正確值填充數(shù)組變量。使用下面的代碼,我認(rèn)為只需將以下行添加到JavaScript代碼中,應(yīng)該非常簡(jiǎn)單:data["__RequestVerificationToken"] = $('[name=__RequestVerificationToken]').val();該<%= Html.AntiForgeryToken() %>是在其正確的位置,動(dòng)作有[ValidateAntiForgeryToken]但是我的控制器動(dòng)作一直在說:“無效的偽造令牌”我在這里做錯(cuò)了什么?碼data["fiscalyear"] = fiscalyear;data["subgeography"] = $(list).parent().find('input[name=subGeography]').val();data["territories"] = new Array();$(items).each(function() {    data["territories"].push($(this).find('input[name=territory]').val());});    if (url != null) {        $.ajax(        {            dataType: 'JSON',            contentType: 'application/json; charset=utf-8',            url: url,            type: 'POST',            context: document.body,            data: JSON.stringify(data),            success: function() { refresh(); }        });    }
查看完整描述

3 回答

?
波斯汪

TA貢獻(xiàn)1811條經(jīng)驗(yàn) 獲得超4個(gè)贊

你不需要因?yàn)镸VC 4. ValidationHttpRequestWrapper解決方案根據(jù)這個(gè)鏈接。


將令牌放在標(biāo)題中。

創(chuàng)建一個(gè)過濾器。

將屬性放在您的方法上。

這是我的解決方案:


var token = $('input[name="__RequestVerificationToken"]').val();

var headers = {};

headers['__RequestVerificationToken'] = token;

$.ajax({

    type: 'POST',

    url: '/MyTestMethod',

    contentType: 'application/json; charset=utf-8',

    headers: headers,

    data: JSON.stringify({

        Test: 'test'

    }),

    dataType: "json",

    success: function () {},

    error: function (xhr) {}

});



[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = false, Inherited = true)]

public class ValidateJsonAntiForgeryTokenAttribute : FilterAttribute, IAuthorizationFilter

{

    public void OnAuthorization(AuthorizationContext filterContext)

    {

        if (filterContext == null)

        {

            throw new ArgumentNullException("filterContext");

        }


        var httpContext = filterContext.HttpContext;

        var cookie = httpContext.Request.Cookies[AntiForgeryConfig.CookieName];

        AntiForgery.Validate(cookie != null ? cookie.Value : null, httpContext.Request.Headers["__RequestVerificationToken"]);

    }

}



[HttpPost]

[AllowAnonymous]

[ValidateJsonAntiForgeryToken]

public async Task<JsonResult> MyTestMethod(string Test)

{

    return Json(true);

}


查看完整回答
反對(duì) 回復(fù) 2019-09-21
?
Smart貓小萌

TA貢獻(xiàn)1911條經(jīng)驗(yàn) 獲得超7個(gè)贊

出問題的是,應(yīng)該處理此請(qǐng)求并用[ValidateAntiForgeryToken]期望標(biāo)記的控制器動(dòng)作期望__RequestVerificationToken與該請(qǐng)求一起被稱為POST 的參數(shù)。


您正在使用的參數(shù)中沒有POST,JSON.stringify(data)它會(huì)將表單轉(zhuǎn)換為JSON表示形式,因此會(huì)引發(fā)異常。


所以我可以在這里看到兩個(gè)可能的解決方案:


數(shù)字1:用于x-www-form-urlencoded代替JSON發(fā)送您的請(qǐng)求參數(shù):


data["__RequestVerificationToken"] = $('[name=__RequestVerificationToken]').val();

data["fiscalyear"] = fiscalyear;

// ... other data if necessary


$.ajax({

    url: url,

    type: 'POST',

    context: document.body,

    data: data,

    success: function() { refresh(); }

});

數(shù)字2:將請(qǐng)求分為兩個(gè)參數(shù):


data["fiscalyear"] = fiscalyear;

// ... other data if necessary

var token = $('[name=__RequestVerificationToken]').val();


$.ajax({

    url: url,

    type: 'POST',

    context: document.body,

    data: { __RequestVerificationToken: token, jsonRequest: JSON.stringify(data) },

    success: function() { refresh(); }

});

因此,在所有情況下,您都需要發(fā)布該__RequestVerificationToken值。


查看完整回答
反對(duì) 回復(fù) 2019-09-21
  • 3 回答
  • 0 關(guān)注
  • 628 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)