我使用 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);
}