jQuery Ajax calls and the Html.AntiForgeryToken()

前端 未结 20 2542
鱼传尺愫
鱼传尺愫 2020-11-22 16:34

I have implemented in my app the mitigation to CSRF attacks following the informations that I have read on some blog post around the internet. In particular these post have

20条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-22 17:22

    first use @Html.AntiForgeryToken() in html

     $.ajax({
            url: "@Url.Action("SomeMethod", "SomeController")",
            type: 'POST',
            data: JSON.stringify(jsonObject),
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            async: false,
            beforeSend: function (request) {
                request.setRequestHeader("RequestVerificationToken", $("[name='__RequestVerificationToken']").val());
            },
            success: function (msg) {
                alert(msg);
            }
    

提交回复
热议问题