在用户获得批准之前验证用户时遇到问题。这是代码:
公共无效登录(LoginViewModel 模型){
if (Membership.ValidateUser(model.UserName, model.Password)) { if (Membership.GetUser(model.UserName).IsApproved) { FormsAuthentication.SetAuthCookie(model.UserName, model.KeepMeLoggedIn); } else { throw new Exception("Your email is not verified yet. Please check your email!"); } } else { throw new Exception("Username or Password doesn't match"); } }
问题是即使用户名和密码都很好,但用户还没有被批准,它会抛出第二个异常。
我试图覆盖 ValidateUser,但我不能调用它。
public class MyValidation : SqlMembershipProvider { public override bool ValidateUser(string username, string password) { MembershipUser user = GetUser(username, false); if (!user.IsApproved) { return true; } return base.ValidateUser(username, password); } }
有什么建议么?
编辑:控制器代码
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Login(LoginViewModel model)
{
AccountService service = new AccountService();
if (ModelState.IsValid)
{
try
{
service.Login(model);
return RedirectToAction("Index", "Home");
}
catch (Exception e)
{
TempData["errorMessage"] = e.Message;
return RedirectToAction("Index", "Home");
}
}
return RedirectToAction("Index", "Home");
}
我提到登录是部分视图