【发布时间】:2013-12-18 02:00:57
【问题描述】:
我最近发布了一个使用 MVC 4 和 Entity Framework 5 构建的 Web 应用程序 Live。 MVC 应用程序使用 Razor 视图。
我注意到使用 Elmah 时,当用户登录应用程序时,有时他们会收到以下错误
The provided anti-forgery token was meant for user "" but the current user is "user"
我已经对如何解决此问题进行了一些研究,但似乎没有什么对我有用。请在下面查看我的登录视图和相应的控制器操作。
剃刀视图
@if (!HttpContext.Current.User.Identity.IsAuthenticated)
{
using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<div class="formEl_a">
<fieldset>
<legend>Login Information</legend>
<div class="lbl_a">
Email
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.Email, new { @class = "inpt_a" })<br />
@Html.ValidationMessageFor(m => m.Email)
</div>
<div class="lbl_a">
@Html.LabelFor(m => m.Password)
</div>
<div class="editor-field sepH_b">
@Html.PasswordFor(m => m.Password, new { @class = "inpt_a" })<br />
@Html.ValidationMessageFor(m => m.Password)
</div>
</fieldset>
</div>
<br />
<p>
<input type="submit" value="Log In" class="btn btn_d sepV_a" />
</p>
}
}
控制器
[AllowAnonymous]
public ActionResult Login()
{
return View();
}
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(LoginModel model, string returnUrl)
{
if (ModelState.IsValid && _accountService.Logon(model.Email, model.Password, true))
{
//Validate
}
else
{
// inform of failed login
}
}
我认为这一切看起来都不错,但错误仍然存在。有没有人对如何解决这个问题有任何想法?
非常感谢您的帮助。
谢谢。
【问题讨论】:
-
你检查这个答案吗? stackoverflow.com/questions/14970102/…
-
如果 AllowAnonymous 从 Login Action 中移除,则不会调用 Login Action(“因为用户尚未登录”)
标签: asp.net-mvc asp.net-mvc-4 razor antiforgerytoken