public virtual MvcMailMessage PasswordReset(MailerModel model) { ViewBag.UserName = model.UserName; ViewBag.Password = model.Password; return Populate(x => { x.Subject = model.Subject; x.ViewName = "PasswordReset"; x.To.Add(model.ToEmail); x.IsBodyHtml = true; }); }
public virtual ActionResult ForgotPassword(ForgotPasswordViewModel model) { string userName = membershipService.GetUserNameByEmail(model.Email); // Get the userName by the email address if (string.IsNullOrEmpty(userName)) { ModelState.AddModelError("Email", "Email address does not exist. Please check your spelling and try again."); return RedirectToAction("ForgotPassword"); } MembershipUser user = membershipService.GetUser(userName); if (user == null) { ModelState.AddModelError("", "The user does not exist. Please check your entry and try again."); return RedirectToAction("ForgotPassword"); } if (model.RequireSecretQuestionAndAnswer && model.Checked == false) { // Get the SecretQuestion model.SecretQuestion = user.PasswordQuestion; model.Checked = true; return RedirectToAction("EnterSecretAnswer", model); } if (model.RequireSecretQuestionAndAnswer && model.Checked == true) { if (string.IsNullOrEmpty(model.SecretAnswer)) { ModelState.AddModelError("SecretAnswer", "The Secret Answer is required."); return RedirectToAction("EnterSecretAnswer", model); } } // Now reset the password string newPassword = string.Empty; if (membershipService.RequiresQuestionAndAnswer) { try { newPassword = user.ResetPassword(model.SecretAnswer); } catch (NullReferenceException) { ModelState.AddModelError("PasswordAnswer", "The Secret Password is required."); return RedirectToAction("EnterSecretAnswer", model); } catch (Exception ex) { ModelState.AddModelError("PasswordAnswer", ex.Message); return RedirectToAction("EnterSecretAnswer", model); } } else { newPassword = user.ResetPassword(); } // Email the new pasword to the user try { SmtpSection smtp = (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp"); // Set the MailerModel properties that will be passed to the MvcMailer object. // Feel free to modify the properties as you need. MailerModel m = new MailerModel(); m.UserName = user.UserName; m.Password = newPassword; m.FromEmail = smtp.From; m.Subject = ConfigSettings.SecurityGuardEmailSubject; m.ToEmail = model.Email; Mailer.PasswordReset(m).Send(); } catch (Exception) { } return RedirectToAction("ForgotPasswordSuccess"); }