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:
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.