您的检查方法非常接近。我已使用以下代码进行登录以检查确认。
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(LoginModel model, string returnUrl)
{
string errorMsg = "The user name or password provided is incorrect.";
if (model.IsConfirmed)
{
if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe))
{
return RedirectToLocal(returnUrl);
}
else if (WebSecurity.FoundUser(model.UserName) && !WebSecurity.IsConfirmed(model.UserName))
{
model.IsConfirmed = false;
errorMsg = "You have not completed the registration process. To complete this process look for the email that provides instructions or press the button to resend the email.";
}
}
else //Need to resend confirmation email
{
ResendConfirmationEmail(model.UserName);
errorMsg = "The registration email has been resent. Find the email and follow the instructions to complete the registration process.";
model.IsConfirmed = true;
}
// If we got this far, something failed, redisplay form
ModelState.AddModelError("", errorMsg );
return View(model);
}
您会注意到主要区别在于您还需要使用FoundUser方法检查用户是否在系统中,否则如果传入错误的用户名IsConfirmed将返回 false。在这种情况下,我添加了一个IsConfirmed属性到查看模型。这在视图中用于确定是否显示一个按钮,该按钮允许用户将确认电子邮件重新发送给他们,以防他们丢失它。您可以在本文中阅读有关此方法的更多详细信息。