【问题标题】:how can i use AntiForgeryToken with JSON post in mvc 4我如何在 mvc 4 中将 AntiForgeryToken 与 JSON 帖子一起使用
【发布时间】:2012-12-16 20:48:42
【问题描述】:

我有使用 JSON.stringify 将数据发布到控制器类的 jQuery 代码,但是当我使用 AntiForgeryToken 时,它不起作用.. 有更好的方法来保护 JSON 发布,否则我错过了一些东西....

其次,我是否需要额外的......即加密以保护 JSON 数据......

非常感谢您在高级方面的帮助...

<script type="text/javascript">

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

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

        $.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);
            }


        });

    });
});

 </script>

@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: true))
        {
            returnString = "user is authenticated";
        }

        else
        { returnString = "Message from loginProcess"; }

        return Json(returnString, JsonRequestBehavior.AllowGet);
    }

【问题讨论】:

    标签: jquery asp.net-mvc json security simplemembership


    【解决方案1】:

    问题是您的请求中没有包含 VerificationToken:

    var _authetication_Data = { _UserName: $('#u1').val(),  _Password: $('#p1').val(), __RequestVerificationToken: $('[name=__RequestVerificationToken]').val(); }
    

    【讨论】:

    • 谢谢 Queti,我收到了 Json 响应,但它是否真的在验证令牌,因为当我在客户端修改令牌代码并且仍在工作时,我猜它不应该?有什么办法可以用正确的令牌确认身份验证???谢谢你
    • var token = $('input[name=__RequestVerificationToken]').val()+"999999";
    【解决方案2】:

    这就是我使用代码的方式

    <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()+"999999";
         //   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);
                    }
                });
    
        });
    });
    

    【讨论】:

      猜你喜欢
      • 2012-12-03
      • 1970-01-01
      • 1970-01-01
      • 2014-05-21
      • 1970-01-01
      • 2012-11-01
      • 2013-05-19
      • 2018-10-29
      • 1970-01-01
      相关资源
      最近更新 更多