【问题标题】:how can test AntiForgeryToken with JSON post in mvc 4如何在 mvc 4 中使用 JSON 帖子测试 AntiForgeryToken
【发布时间】:2012-12-03 21:53:56
【问题描述】:

我使用 MVC 4 Visual Studio 2012 将 Json 发布到控制器...我已成功将 json 数据与 AntiForgeryToken 一起传递给控制器​​,但我不知道如何准确测试它是否真的在工作“AntiForgeryToken 的正确性”。我还尝试在客户端的 __RequestVerificationToken 代码中添加 9999 以查看它是否在服务器端进行验证,并且确实如此!!!。我的猜测是,如果我是正确的,它不应该????这是我的代码

<script type="text/javascript">

$(document).ready(function (options) {
    $('#id_login_submit').click(function () {

        var token = $('input[name=__RequestVerificationToken]').val();

//var token = $('input[name=__RequestVerificationToken]').val()+"99999";
//   alert("token :: "+token);

        var _authetication_Data = { _UserName: $('#u1').val(), _Password: $('#p1').val(), "__RequestVerificationToken": token }


            $.ajax({
                type: "POST",
                url: "/Account/ProcessLoginRequest",
                data: JSON.stringify({ model: _authetication_Data }),
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                success: function (response) {
                    alert(response);
                }
            });

    });
});

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    @Html.LabelFor(m => m._UserName)
    @Html.TextBoxFor(m => m._UserName, new { id = "u1"})


    @Html.LabelFor(m => m._Password)
    @Html.PasswordFor(m => m._Password, new { id = "p1"})


    <input type="button" id="id_login_submit" value="Login" />
}

   [HttpPost]
   [ValidateAntiForgeryToken]
    public JsonResult ProcessLoginRequest(LoginModel model)
    {
        string returnString = null;


      if (ModelState.IsValid && WebSecurity.Login(model._UserName, model._Password, persistCookie: false))
        {
            returnString = "user is authenticated";     
        }

        else
        { returnString = "user not authenticated"; }

        return Json(returnString, JsonRequestBehavior.AllowGet);
    }

【问题讨论】:

  • 旁注:请确保您始终将您的活动挂在表单提交上,而不是按下按钮。这是因为您仍然可以提交表单,但在文本输入中按 Enter。

标签: asp.net-mvc json antiforgerytoken simplemembership


【解决方案1】:

它对我有用,实际上我没有使用表单,这是我的代码:

查看代码:

var token = $('input[name=__RequestVerificationToken]').val();        

        $.post(url, { Telefono: telefono, MensajeSMS: mensajeSMS, __RequestVerificationToken : token }, ...............

控制器方法,只需使用 apropiate 属性签名:

[验证反伪造令牌] public JsonResult jsonEnviarSMS(string Telefono, string MensajeSMS)

【讨论】:

    【解决方案2】:

    是的,您可以...但您可以尝试使用serialize() 方法。像这样的:

    $.ajax({
                    type: "POST",
                    url: "/Account/ProcessLoginRequest",
                    data: $("#your_form_id").serialize(),
                    dataType: "json",
                    contentType: "application/json; charset=utf-8",
                    success: function (response) {
                        alert(response);
                    }
                });
    

    当您使用serialize 方法时,这会将form 标记内的所有元素序列化为一个数据数组,例如{ field: value, field2: value2, field3: value3 },并且Token 将是一个隐藏的输入,因此,它将打开序列化结果。

    有关更多信息,请查看文档:http://api.jquery.com/serialize/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-29
      • 2012-06-06
      • 2020-05-08
      • 1970-01-01
      • 2013-06-06
      • 2016-07-28
      • 2018-05-27
      • 2014-04-18
      相关资源
      最近更新 更多