我如何在mvc 4中使用带有JSONpost的AntiForgeryToken

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

其次我需要额外的这个…即加密来保护JSON数据……

非常感谢先进的帮助……

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

 @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"})  } 

  [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); } 

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

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

这就是我如何使用代码