【问题标题】:The required anti-forgery form field "__RequestVerificationToken" is not present on $.post$.post 上不存在所需的防伪表单字段“__RequestVerificationToken”
【发布时间】:2018-04-25 21:38:56
【问题描述】:

目前我的问题是我的 AntiForgeryToken 没有出现在我的通话后,但据我所知,它确实存在。

由于我不使用表单从 HTML 中获取数据,而只是输入字段,因此我在页面底部创建了一个空表单:

@using (Html.BeginForm(null, null, FormMethod.Post, new { id = 
"__AjaxAntiForgeryForm" }))
{
    @Html.AntiForgeryToken()
}

这会产生一个我可以使用 jQuery 获得的 AntiForgeryToken。

所以在我的 Javascript 中我会这样做:

                        var LoginData = {
                            EmailAddress: currentMail,
                            Password: password
                        }
                        var form = $('#__AjaxAntiForgeryForm');
                        var token = $('input[name="__RequestVerificationToken"]', form).val();

                        data = {
                            __RequestVerificationToken: token,
                            LoginData: LoginData
                        }

                        $.post(window.location,
                            {
                                scController: '*Controller*',
                                scAction: 'ValidateLogin',
                                data: data
                            }).done(function (d, e) { 
                                console.log("done");
                                console.log(d);
                                console.log(e);
                            }).fail(function (d, e) {
                                console.log("error");
                                console.log(d);
                                console.log(e);
                            });

我创建的数据对象导致:

{LoginData: {EmailAddress: "********", Password: "*******"}, __RequestVerificationToken: "Imagine a token here"}

然后是我的控制器操作:

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ResultMessage ValidateLogin(LoginData login)
    {
        return _userRepository.Login(login);
    }

由于某种原因,当我尝试写这篇文章时,我收到了这个错误:

“所需的防伪表单字段“__RequestVerificationToken”不存在。”

我做错了什么?

编辑 1: 我看到 Cookie 标头中的 __RequestVerificationToken 与我随数据发送的不同。这怎么可能?

【问题讨论】:

  • 是否发送了 AntiForgeryToken cookie?为什么不使用标准表格?
  • @Mik 是的,在请求标头中有一个 Cookie 标头,其中包括:__RequestVerificationToken=xxxxxxxx;而且因为输入在 SweetAlert 中,所以我不能/不想 - 在那里使用表单。
  • @Mik 我看到 Cookie 标头中的 __RequestVerificationToken 与我随数据发送的不同。这怎么可能?
  • 我不认为他们应该是平等的。生成两个值。 stackoverflow.com/questions/20911470/…

标签: jquery asp.net ajax antiforgerytoken


【解决方案1】:

https://docs.microsoft.com/en-us/aspnet/web-api/overview/security/preventing-cross-site-request-forgery-csrf-attacks 所述,与 ajax 一起使用时,防伪令牌必须在标头中发送,如下所示:

$.ajax("api/values", {
    type: "post",
    contentType: "application/json",
    data: {  }, // JSON data goes here
    dataType: "json",
    headers: {
        'RequestVerificationToken': <Token>
    }
});

【讨论】:

  • 似乎没有解决我的问题,更改请求后我仍然收到同样的错误
【解决方案2】:

这是我在这个问题上的分步方法。我正在使用 angularJS、jquery、ASP.NET MVC 5 https://stackoverflow.com/a/57781976/2508781

使用 MVC 的 AntiForgery.Validate() 可以在这里验证防伪令牌的价值,并且到目前为止非常棒。希望这可以帮助!

【讨论】:

    猜你喜欢
    • 2014-07-13
    • 2016-05-26
    • 2016-03-27
    • 2015-03-14
    • 2017-12-26
    • 2016-09-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多