在我的 MVC 项目中,验证器无意中要求在登录时选中“记住我”复选框。在 IE8 上,除非用户勾选“记住我”框,否则它将不允许用户登录。所有其他浏览器都很好,包括 IE7。
<input id="RememberMe" type="checkbox" value="true" name="RememberMe" data-val-required="The Remember me? field is required." data-val="false">
我试图将实体框架中的属性更改为可为空的布尔值?但这会产生错误:
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; }
}
这是进行登录的控制器,你可以看到它是开箱即用的 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);
}
这是视图:
@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>