【问题标题】:Confirm email doesn't work确认电子邮件不起作用
【发布时间】:2015-05-12 09:11:35
【问题描述】:

我正在处理一个 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);
        }
    }

问候。 卡尔松。

【问题讨论】:

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


    【解决方案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()
    ...
    ...
    

    【讨论】:

    • 啊,我知道你在那里做了什么。这确实解决了我的问题,非常感谢。
    猜你喜欢
    • 2018-04-18
    • 2020-12-25
    • 2015-07-14
    • 2015-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多