jQuery Ajax calls and the Html.AntiForgeryToken()

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

    function DeletePersonel(id) {
    
        var data = new FormData();
        data.append("__RequestVerificationToken", "@HtmlHelper.GetAntiForgeryToken()");
    
        $.ajax({
            type: 'POST',
            url: '/Personel/Delete/' + id,
            data: data,
            cache: false,
            processData: false,
            contentType: false,
            success: function (result) {
            }
        });
    }
    
    public static class HtmlHelper {
        public static string GetAntiForgeryToken() {
            System.Text.RegularExpressions.Match value = 
                    System.Text.RegularExpressions.Regex.Match(System.Web.Helpers.AntiForgery.GetHtml().ToString(), 
                            "(?:value=\")(.*)(?:\")");
            if (value.Success) {
                return value.Groups[1].Value;
            }
            return "";
        }
    }
    

提交回复
热议问题