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

后端 未结 3 1466
一生所求
一生所求 2020-12-20 20:20

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 kep

3条回答
  •  无人及你
    2020-12-20 20:58

    When you call CallAjax(), where is data coming from? I ask because, usually when your data comes from a form then your CSRF token is already part of the form, typically in a hidden field.

    .... other form fields ....

    So if your data is all coming from a form, then you should just make sure that the token is a hidden part of that form and the token should automatically be included.

    If your data is coming from somewhere other than a form, then it is understandable that you would stash your token somewhere and then include it after the data has been assembled. But you might consider adding the token to the data, rather than building a new object from the token and then adding all the data to it.

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

提交回复
热议问题