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

时间:2015-05-12 13:02:00

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

正如标题所说,如果用户丢失密码,我的用户就无法恢复密码。出于某种原因,它不会发送电子邮件。我们确认他们的电子邮件地址比收到电子邮件时更有用。

以下是代码:

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

以下是他们想要恢复密码的代码:

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

但由于某些原因,这不起作用,并且他们无法收到有关如何重置密码的电子邮件。

1 个答案:

答案 0 :(得分:0)

假设您正在使用MVC的开箱即用模板,那么:

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

应该是:

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

因为输入框要求提供电子邮件,而不是用户名。

然后,除非您将网站设置为要求确认电子邮件,否则该行:

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

应该是

if (user == null)

这至少会让您看到发送电子邮件的代码。我今天刚遇到同样的问题,上面的修改为我解决了这个问题。