区分无效密码和未确认帐户的最佳方法



判断帐户未被确认与密码无效的最佳方法是什么。

这是我的场景:

  1. 用户寄存器
  2. 已创建确认令牌并发送了一封电子邮件
  3. 用户尝试登录

在这一点上,我们不知道用户是否输入了无效的密码&尚未确认。

我有这段代码,但对我来说似乎不对。

  {
      if (ModelState.IsValid)
      {
        if (WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe))
        {
          return RedirectToLocal(returnUrl);
        }
        else if (WebSecurity.IsConfirmed(model.UserName) == false)
        {
          ModelState.AddModelError("", "The user account was not confirmed. Please check your email and try again");
          return View(model);
        }
      }

必须有一种方法可以做得更干净。有什么想法吗?

谢谢,

您的检查方法非常接近。我使用了以下代码进行登录,以检查确认。

    [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属性。这在视图中用于确定是否显示一个按钮,该按钮允许用户在丢失确认电子邮件时重新发送给他们。您可以在本文中阅读有关此方法的更多详细信息。

我能想到的最简单的方法是在Confirmation Token登录之前使用它激活帐户。

或者,如果您想同时使用令牌和密码进行确认然后进行身份验证,我建议您获取密码,对其进行哈希,并将其与数据库版本进行比较。在这个阶段,它将是一个直接的纯文本比较。如果tokenuserid匹配,并且hashed password也与database password匹配,则可以进行身份验证。

如果您需要实际的代码,请告诉我。

相关内容

最新更新