jQuery Ajax calls and the Html.AntiForgeryToken()

前端 未结 20 2574
鱼传尺愫
鱼传尺愫 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:18

    AntiforgeryToken is still a pain, none of the examples above worked word for word for me. Too many for's there. So I combined them all. Need a @Html.AntiforgeryToken in a form hanging around iirc

    Solved as so:

    function Forgizzle(eggs) {
        eggs.__RequestVerificationToken =  $($("input[name=__RequestVerificationToken]")[0]).val();
        return eggs;
    }
    
    $.ajax({
                url: url,
                type: 'post',
                data: Forgizzle({ id: id, sweets: milkway }),
    });
    

    When in doubt, add more $ signs

提交回复
热议问题