确认电子邮件无效

时间:2015-05-12 09:11:36

标签: c# asp.net asp.net-mvc asp.net-mvc-5

我正在使用asp.net mvc5项目,我希望我的用户在登录我的网页之前确认他们的电子邮件。我确实设法得到它,所以当用户注册用户将收到一封电子邮件。我也设法做到这一点,当用户点击电子邮件中的链接时,将在数据库中确认。

所以,问题。我希望他们在登录之前确认他们的电子邮件。 这是我的代码试图实现这一目标。

 // POST: /Account/Login
    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
    {
        if (!ModelState.IsValid)
        {
            var user = await UserManager.FindAsync(model.Username, model.Password); if (user != null)
            {
                if (user.EmailConfirmed == true)
                {
                    await SignInAsync(user, model.RememberMe); return RedirectToLocal(returnUrl);
                }
                else
                {
                    ModelState.AddModelError("", "Confirm Email Address.");
                }
            }
            else
            {
                ModelState.AddModelError("", "Invalid username or password.");
            }
        }

        // This doesn't count login failures towards account lockout
        // To enable password failures to trigger account lockout, change to shouldLockout: true
        var result = await SignInManager.PasswordSignInAsync(model.Username, model.Password, model.RememberMe, shouldLockout: false);
        switch (result)
        {
            case SignInStatus.Success:
                return RedirectToLocal(returnUrl);
            case SignInStatus.LockedOut:
                return View("Lockout");
            case SignInStatus.RequiresVerification:
                return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
            case SignInStatus.Failure:
            default:
                ModelState.AddModelError("", "Invalid login attempt.");
                return View(model);
        }
    }

但是用户无论如何都可以登录而无需确认他们的电子邮件。

以下是我发送电子邮件的方式。

    if (ModelState.IsValid)
    {
        var user = new ApplicationUser() { UserName = model.Username };
        user.Email = model.Email;
        user.EmailConfirmed = false;
        var result = await UserManager.CreateAsync(user, model.Password);
        if (result.Succeeded)
        {
            MailMessage m = new MailMessage(
                new MailAddress("noreply@stuff.net", "Web Registration"),
                new MailAddress(user.Email));
            m.Subject = "Email confirmation";
            m.Body = string.Format("Dear {0}<BR/>Thank you for your registration, please click on the below link to complete your registration: <a href=\"{1}\" title=\"User Email Confirm\">{1}</a>", user.UserName, Url.Action("ConfirmEmail", "Account", new { Token = user.Id, Email = user.Email }, Request.Url.Scheme));
            m.IsBodyHtml = true;
            SmtpClient smtp = new SmtpClient("mail.stuff.net");
            smtp.Credentials = new NetworkCredential("noreply@stuff.net", "passwordstuff");
            smtp.EnableSsl = false;
            smtp.Port = 8889;
            smtp.Send(m);
            return RedirectToAction("ConfirmEmail", "Account", new { Email = user.Email });
        }
        else
        {
            AddErrors(result);
        }
    }

的问候。 卡尔森。

1 个答案:

答案 0 :(得分:1)

代码

var user = await UserManager.FindAsync(model.Username, model.Password); if (user != null)
            {
                if (user.EmailConfirmed == true)
                {
                    await SignInAsync(user, model.RememberMe); return RedirectToLocal(returnUrl);
                }
                else
                {
                    ModelState.AddModelError("", "Confirm Email Address.");
                }
            }
            else
            {
                ModelState.AddModelError("", "Invalid username or password.");
            }

必须执行ModelState.IsValid!当表单数据不正确时,执行替代方法

试试这个:

if (!ModelState.IsValid)
{
    return View();
}

var user = await UserManager.FindAsync()
...
...