简体   繁体   English

用户无法恢复其密码asp.net mvc5

[英]Users can't recover their password asp.net mvc5

As the title says my users can't recover their password if they lose it. 如标题所述,如果我的用户丢失了密码,便无法恢复。 For some reason it doesn't send the email. 由于某种原因,它不发送电子邮件。 I have it working when they are going to confirm their email address than they do receives an email. 当他们要确认其电子邮件地址时,我的工作比他们收到的电子邮件有效。

Here is the code for that: 这是该代码:

    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);
    }
}

And here is the code for when they want to recover their password: 这是他们想要恢复密码时的代码:

            if (ModelState.IsValid)
        {
            var user = await UserManager.FindByNameAsync(model.Email);
            if (user == null || !(await UserManager.IsEmailConfirmedAsync(user.Id)))
            {
                // Don't reveal that the user does not exist or is not confirmed
                return View("ForgotPasswordConfirmation");
            }

            string code = await UserManager.GeneratePasswordResetTokenAsync(user.Id);

            MailMessage m = new MailMessage(
            new MailAddress("noreply@stuff.net", "Web Registration"),
            new MailAddress(user.Email));
            m.Subject = "Forgotten Password";
            m.Body = string.Format("Dear {0}<BR/>Please click on the below link to reset your password: <a href=\"{1}\" title=\"User Forgotten Password\">{1}</a>", user.UserName, Url.Action("ResetPassword", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme));
            m.IsBodyHtml = true;
            SmtpClient smtp = new SmtpClient("mail.stuff.net");
            smtp.Credentials = new NetworkCredential("noreply@stuff.net", "stuff");
            smtp.EnableSsl = false;
            smtp.Port = 8889;
            smtp.Send(m);
            return RedirectToAction("ForgotPasswordConfirmation", "Account");
        }

But for some reason this doesn't work, and they won't get an email on how to reset their password. 但是由于某种原因,这是行不通的,他们也不会收到有关如何重设密码的电子邮件。

Assuming that you are using MVC's out-of-the-box template, then: 假设您正在使用MVC的现成模板,那么:

var user = await UserManager.FindByNameAsync(model.Email);

should really be: 确实应该是:

var user = await UserManager.FindByEmailAsync(model.Email);

because the input box asks for email, not user name. 因为输入框要求输入电子邮件,而不是用户名。

Then, unless you have set the site to require email confirmation, the line: 然后,除非您将站点设置为需要电子邮件确认,否则该行:

if (user == null || !(await UserManager.IsEmailConfirmedAsync(user.Id)))

should be 应该

if (user == null)

This will at least get you to the codes that sends out the email. 这至少会使您了解发送电子邮件的代码。 I just ran into the same issue today, and the above changes solved it for me. 我今天遇到了同样的问题,以上更改为我解决了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM