如何检查“用户”是否有一个确认的电子邮件内ASP.NET身份(Web窗体)?

mrphzbgm  于 2023-01-10  发布在  .NET
关注(0)|答案(3)|浏览(138)

在我的登录页面上,我想实现一个系统,如果用户存在,但没有确认的电子邮件(IsEmailConfirmed),用户需要验证/确认电子邮件。
我没有任何问题重新发送确认码,我的问题是在哪里把声明,以及如何确保用户输入正确的用户名和密码(用户应该是有效的).

登录(代码隐藏)

protected void LogIn(object sender, EventArgs e)
{
  // Validate the user password
  var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
  var signinManager = Context.GetOwinContext().GetUserManager<ApplicationSignInManager>();

  // Require the user to have a confirmed email before they can log on.
  var user = manager.FindByName(username.Text);

  if (IsValid)
   {
    if (user != null)
     {
      // This doen't count login failures towards account lockout
      // To enable password failures to trigger lockout, change to shouldLockout: true
      var result = signinManager.PasswordSignIn(username.Text, Password.Text, RememberMe.Checked, shouldLockout: true);

      switch (result)
       {
        case SignInStatus.Success:
        IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], 
                                                           Response);
        break;

        case SignInStatus.LockedOut:
         //Response.Redirect("/Account/Lockout");    
         FailureText.Text = "This account has been locked out, please try again later.";
         ErrorMessage.Visible = true;
         return;

        case SignInStatus.RequiresVerification:
          Response.Redirect(String.Format("/Account/TwoFactorAuthenticationSignIn?ReturnUrl={0}&RememberMe={1}",
          Request.QueryString["ReturnUrl"],
          RememberMe.Checked),
          true);
          break;

         case SignInStatus.Failure:
         default:
          FailureText.Text = "Invalid login attempt";
          ErrorMessage.Visible = true;
          break;    
          }   
        }
      }    
  else
  {
     FailureText.Text = "Account not found.";
     ErrorMessage.Visible = true;
  }

  //else if (IsValid & !manager.IsEmailConfirmed(user.Id))
  //{
     //    ScriptManager.RegisterStartupScript(this, this.GetType(), "LaunchServerSide", "$(function() { OpenLoginModal(); });", true);
        //    LoginModalTitle.Text = "Account Verification".ToUpper();
        //    LoginModalDetails.Text = "You must have a confirmed email account.";
        //    //ErrorMessage.Visible = true;
        //    //ResendConfirm.Visible = true;
        //}
 }

我感谢你为我的问题找到解决办法所做的努力

5vf7fwbs

5vf7fwbs1#

如果我理解正确的话:在我们检查帐户是否处于活动状态之前,您要确保用户名和密码都正确吗?

protected void LogIn(object sender, EventArgs e)
{
    var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
    var signinManager = Context.GetOwinContext().GetUserManager<ApplicationSignInManager>();
    var user = manager.FindByName(username.Text);

    if (IsValid)
    {
        if (user != null)
        {
            var result = signinManager.PasswordSignIn(username.Text, Password.Text, RememberMe.Checked, shouldLockout: true);

            // If username and password is correct check if account is activated.
            if(!user.EmailConfirmed && result == SignInStatus.Success)
            {
                FailureText.Text = "Invalid login attempt. You must have a confirmed email account.";
                ErrorMessage.Visible = true;
                return;
            }        

            switch (result)
            {
                case SignInStatus.Success:
                    IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], 
                                                           Response);
                    break;
                case SignInStatus.LockedOut:
                    //Response.Redirect("/Account/Lockout");    
                    FailureText.Text = "This account has been locked out, please try again later.";
                    ErrorMessage.Visible = true;
                    return;

                case SignInStatus.RequiresVerification:
                    Response.Redirect(String.Format("/Account/TwoFactorAuthenticationSignIn?ReturnUrl={0}&RememberMe={1}",
                                                    Request.QueryString["ReturnUrl"],
                                                    RememberMe.Checked),
                                                    true);
                    break;
                case SignInStatus.Failure:
                default:
                    FailureText.Text = "Invalid login attempt";
                    ErrorMessage.Visible = true;
                    break;    
            }                
        }    
        else
        {
            FailureText.Text = "Account not found.";
            ErrorMessage.Visible = true;
        }
    }
}
72qzrwbm

72qzrwbm2#

要检查用户是否已确认,需要做一些小的修改,您需要检查IsEmailConfirmed属性,以查看用户是否已确认帐户。
This article很好地解释了流程以及如何执行这些操作。下面的代码片段摘自那篇文章。

var user = manager.FindByName(Email.Text);
    if (user != null)
    {
        if (!user.EmailConfirmed)
        {
            FailureText.Text = "Invalid login attempt. You must have a confirmed email address. Enter your email and password, then press 'Resend Confirmation'.";
            ErrorMessage.Visible = true;
            ResendConfirm.Visible = true;
        }
        else
        {
             // your other logic goes here if the user is confirmed.
             ....
        }
    }
    else 
    {
        // user does not exist.
    }
cygmwpex

cygmwpex3#

在.NET 7中登录. cshtml.cs

if (result.IsLockedOut)
            {
                _logger.LogWarning("User account locked out.");
                return RedirectToPage("./Lockout");
            }
            if (result.IsNotAllowed)
            {
                var user = _signInManager.UserManager.FindByEmailAsync(Input.Email);
                if (user != null)
                {
                    if (!user.Result.EmailConfirmed)
                    {
                        ModelState.AddModelError(string.Empty, "Account isn't active, Get Your Email & click on Active link, ");
                    }
                }
                else
                {
                    ModelState.AddModelError(string.Empty, "incorrect username or password");//Email not found
                }
            }
            else
            {
                ModelState.AddModelError(string.Empty, "incorrect username or password");
                return Page();
            }

相关问题