Пример #1
0
 /// <summary>
 /// This allows the non-logged on user to have his password
 /// reset and emailed to him.
 /// </summary>
 /// <returns></returns>
 public ActionResult ForgotPassword()
 {
     var viewModel = new ForgotPasswordViewModel()
     {
         RequiresQuestionAndAnswer = membershipService.RequiresQuestionAndAnswer
     };
     return View(viewModel);
 }
        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");
        }
Пример #3
0
        public ActionResult ForgotPassword(ForgotPasswordViewModel model)
        {
            // Get the userName by the email address
            string userName = membershipService.GetUserNameByEmail(model.Email);

            // Get the user by the userName
            MembershipUser user = membershipService.GetUser(userName);

            // Now reset the password
            string newPassword = string.Empty;

            if (membershipService.RequiresQuestionAndAnswer)
            {
                newPassword = user.ResetPassword(model.PasswordAnswer);
            }
            else
            {
                newPassword = user.ResetPassword();
            }

            // Email the new pasword to the user
            try
            {
                string body = BuildMessageBody(user.UserName, newPassword, ConfigSettings.SecurityGuardEmailTemplatePath);
                Mail(model.Email, ConfigSettings.SecurityGuardEmailFrom, ConfigSettings.SecurityGuardEmailSubject, body, true);
            }
            catch (Exception)
            {
            }

            return RedirectToAction("ForgotPasswordSuccess");
        }
 /// <summary>
 /// This is the GET action to collect the answer and then continue.
 /// This is only hit if the web.config/system.web/membership provider is
 /// set with the attribute requiresQuestionAndAnswer="true".
 /// </summary>
 /// <param name="model">ForgotPasswordViewModel</param>
 /// <returns></returns>
 public virtual ActionResult EnterSecretAnswer(ForgotPasswordViewModel model)
 {
     return View(model);
 }