Pat*_*ick 9 security asp.net-mvc jquery razor
在成功登录同一页面中的另一个视图后,我需要在视图中的表单中重新加载AntiForgeryToken.
我可以通过jQuery在结果登录页面输入@ Html.AntiForgeryToken()键的形式更新吗?
如果是,这是推荐和安全吗?
我该怎么做?
编辑:
在布局中我有不同的PartialViews:
部分登录:
<ul class="menudrt" id="headerLogin">
@{ Html.RenderAction(MVC.Account.LoginHeader()); }
</ul>
Run Code Online (Sandbox Code Playgroud)
在另一个部分,发送评论的可能性:
<div class="comentsform">
<!-- Comments form -->
@{ Html.RenderAction(MVC.Comment.Create()); }
</div>
Run Code Online (Sandbox Code Playgroud)
要发送评论,用户必须登录,因此在登录后,评论表格需要更新AntiForgeryToken或者我得到验证错误,因为现在登录已经不同了.
谢谢
Dar*_*rov 21
出现此问题的原因是AntiForgery令牌包含当前经过身份验证的用户的用户名.
所以这是发生的事情:
所以你有几个选择来解决这个问题:
解决方案1的显而易见性并不能使它成为我在答案中覆盖它的好方法.让我们看看第二个解决方案是如何实现的.
但首先让我们用一个例子重现问题:
控制器:
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Login()
{
FormsAuthentication.SetAuthCookie("john", false);
return Json(new { success = true });
}
[HttpPost]
[ValidateAntiForgeryToken()]
public ActionResult Comment()
{
return Content("Thanks for commenting");
}
}
Run Code Online (Sandbox Code Playgroud)
~/Views/Home/Index.cshtml:
<div>
@{ Html.RenderPartial("_Login"); }
</div>
<div id="comment">
@{ Html.RenderPartial("_Comment"); }
</div>
<script type="text/javascript">
$('#loginForm').submit(function () {
$.ajax({
url: this.action,
type: this.method,
data: $(this).serialize(),
success: function (result) {
alert('You are now successfully logged in');
}
});
return false;
});
</script>
Run Code Online (Sandbox Code Playgroud)
~/Views/Home/_Login.cshtml:
@using (Html.BeginForm("Login", null, FormMethod.Post, new { id = "loginForm" }))
{
@Html.AntiForgeryToken()
<button type="submit">Login</button>
}
Run Code Online (Sandbox Code Playgroud)
~/Views/Home/_Comment.cshtml:
@using (Html.BeginForm("Comment", null, FormMethod.Post))
{
@Html.AntiForgeryToken()
<button type="submit">Comment</button>
}
Run Code Online (Sandbox Code Playgroud)
好了,现在当您导航到Home/Index时,将呈现相应的视图,如果您按下Comment按钮而不先登录,它将起作用.但如果您登录然后注释它将失败.
所以我们可以添加另一个控制器动作,它将返回一个带有简单Html.AntiForgeryToken调用的局部视图,以生成一个新的令牌:
public ActionResult RefreshToken()
{
return PartialView("_AntiForgeryToken");
}
Run Code Online (Sandbox Code Playgroud)
和相应的partial(~/Views/Home/_AntiForgeryToken.cshtml):
@Html.AntiForgeryToken()
Run Code Online (Sandbox Code Playgroud)
最后一步是通过更新我们的AJAX调用来刷新令牌:
<script type="text/javascript">
$('#loginForm').submit(function () {
$.ajax({
url: this.action,
type: this.method,
data: $(this).serialize(),
success: function (result) {
$.get('@Url.Action("RefreshToken")', function (html) {
var tokenValue = $('<div />').html(html).find('input[type="hidden"]').val();
$('#comment input[type="hidden"]').val(tokenValue);
alert('You are now successfully logged in and can comment');
});
}
});
return false;
});
</script>
Run Code Online (Sandbox Code Playgroud)
您可以通过在登录后返回AntiForgeryToken来实现此目的.
无需重复使用相同的令牌2次.
控制器:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Login(LoginModel model)
{
// do something with login
// return new token as a partial to parse and get value
return this.PartialView("_AntiForgeryPartial");
}
Run Code Online (Sandbox Code Playgroud)
_AntiForgeryPartial:
@Html.AntiForgeryToken()
Run Code Online (Sandbox Code Playgroud)
您可以使用与此类似的JS仅将新的AntiForgeryToken值加载到注释表单中.
视图:
$("#LoginForm").submit(function (e) {
e.preventDefault();
var $this = $(this);
$.ajax({
type: $this.attr("method"),
url: $this.attr("action"),
data: $this.serialize(),
success: function (response) {
// get the new token from the response html
var val = $(response).find('input[type="hidden"]').val();
// set the new token value
$('.commentsform input[type="hidden"]').val(val);
}
});
});
Run Code Online (Sandbox Code Playgroud)
当评论表单执行POST时,您应该能够针对新的唯一AntiForgeryToken进行验证.
史蒂文桑德森有一个很棒的帖子,AntiForgeryToken()如果你想了解更多关于如何使用它以及它的用途.