public ActionResult ForgotPassword(ForgotPasswordModel forgotPassword) { var userAccount = this.db.Accounts.Where(a => a.EmailAddress == forgotPassword.EmailAddress); if(userAccount.Count() == 0) { ModelState.AddModelError(string.Empty, "Email address not found"); } if(ModelState.IsValid) { string newPassword = GetRandomHexPassword(); this.account = userAccount.Single(); this.account.Password = account.GetPasswordHash(newPassword); this.db.Entry(this.account).State = EntityState.Modified; this.db.SaveChanges(); // Send email notification this.emailNotificationHelper.SendPasswordResetNotification(newPassword, forgotPassword, "ResetPassword"); return RedirectToAction("ResetPasswordSuccess"); } else { return View(); } }
public void SendPasswordResetNotification(string password, ForgotPasswordModel forgotPassword, string emailView) { dynamic email = new Email(emailView); email.UserEmailAddress = forgotPassword.EmailAddress; email.Password = password; try { email.Send(); } catch { // Need to log when it fails, the email type and information } }