Asp.net mvc 如何避免未经验证的电子邮件登录asp.net mvc

Asp.net mvc 如何避免未经验证的电子邮件登录asp.net mvc,asp.net-mvc,authentication,Asp.net Mvc,Authentication,在我编写代码的时候: await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false); 用户已登录,即使没有经过验证的电子邮件。 我需要在哪里配置Signin manager以拒绝使用未经验证的电子邮件登录? 或者我必须调查数据库,看看有关的信息 我一直在使用的解决方案是在登录POST期间防止SignInAsync,是的,这将使用db信息 在login POST方法中,只需在执行SignInAsy

在我编写代码的时候:

await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);
用户已登录,即使没有经过验证的电子邮件。 我需要在哪里配置Signin manager以拒绝使用未经验证的电子邮件登录?
或者我必须调查数据库,看看有关的信息

我一直在使用的解决方案是在登录POST期间防止SignInAsync,是的,这将使用db信息

在login POST方法中,只需在执行SignInAsync之前为Email Verified属性添加布尔检查。您不必完全复制下面的代码,只需添加布尔检查

   [HttpPost]
   public ActionResult YourLoginMethod(loginModel model){
      // get user
      var user = db.ApplicationUsers.Where(a=>a.Key == model.Key).FirstOrDefault();

      if(user!=null){
         if(user.IsEmailVerified){
            // if the email verified property is true call SignInAsync
            var result = await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);
            // .. code redirect here
         }else{
            // tell the user that his email is not verified return to a view with error
            return View();
         }
      }

      // cant find user
      return View();
   }

或者,如果您想修改SignInManager,实际上可以从它继承并重写SignInAsync,因为正如中所述,SignInAsync是虚拟的

   public class CustomSignInManager:SignInManager{
      public override Task SignInAsync(...){
         // check for user email verified first with db context

         if(emailIsVerified){
            // call base class implementation
            base.SignInAsync(...)
         }
      }
   }