在我的 MVC 项目中,验证器无意中要求在登录时选中 RememberMe 复选框。在 IE8 上,除非用户勾选 Remember Me 框,否则将不允许用户登录。所有其他浏览器都可以,包括 IE7。
<input id="RememberMe" type="checkbox" value="true" name="RememberMe" data-val-required="The Remember me? field is required." data-val="false">
我尝试将 Entity Framework 中的属性更改为可为空的 bool 值?但这会产生错误:
The best overloaded method match for 'WebMatrix.WebData.WebSecurity.Login(string, string, bool)' has some invalid argument
所以,看起来我无法更改它来解决这个问题。我也尝试过用 jQuery 重写该属性,但它又变回来了。这确实是随机的,我很惊讶这被忽视了,因为它是开箱即用的技术。任何帮助将不胜感激。
这是登录模型:
public class LoginModel
{
[EmailAddressAttribute]
[Required]
[StringLength(80)]
[Display(Name = "Email Address")]
public string UserName { get; set; }
[Required]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[Display(Name = "Remember me?")]
public bool RememberMe { get; set; }
}
这是执行登录的 Controller ,您可以看到它是开箱即用的 MVC 东西:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(LoginModel model, string returnUrl)
{
if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe))
{
return RedirectToLocal(returnUrl);
}
// If we got this far, something failed, redisplay form
ModelState.AddModelError("", "The user name or password provided is incorrect.");
return View(model);
}
这是 View :
@Html.ValidationSummary(true)
<ol class="form">
<li>
@Html.LabelFor(m => m.UserName)
@Html.TextBoxFor(m => m.UserName, new { id="txUserName" })
@Html.ValidationMessageFor(m => m.UserName)
</li>
<li>
@Html.LabelFor(m => m.Password)
@Html.PasswordFor(m => m.Password)
@Html.ValidationMessageFor(m => m.Password)
</li>
<li>
@Html.LabelFor(m => m.RememberMe, new { @class = "checkbox" })
@Html.CheckBoxFor(m => m.RememberMe)
</li>
<li class="form-buttons">
<input type="submit" value="Log in" /> @Html.ActionLink("Forgot Password", "ForgotPassword", "Account")
</li>
</ol>
最佳答案
经过一番研究,据说这是由 Internet Explorer 10 中的 IE7 和 IE8 兼容模式引起的。
在相同条件下运行不引人注目的 jquery 验证也会导致其他问题。
关于asp.net-mvc - MVC RememberMe 验证故障,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18099478/