Exemplo n.º 1
0
        public ActionResult ForgotPassword(string email, string returnUrl)
        {
            if (email.Length > 0)
            {
                if (Utils.Validate.EmailAddress(email))
                {
                    DB.Context db = new DB.Context();
                    Account acc = db.Accounts.SingleOrDefault(x => x.Email.Equals(email));
                    if (acc != null)
                    {
                        string randomPassword = Security.Password.GenerateRandom();
                        string password = Security.Password.GenerateHash(email, randomPassword);

                        acc.Password = password;

                        try
                        {
                            db.SaveChanges();
                            //send email reminder
                            Utils.Email.sendEmail(Config.ActiveConfiguration.Mail.From, acc.Email, "Password Reminder", "Your new password is: " + randomPassword, true, Config.ActiveConfiguration.Mail.Host, Config.ActiveConfiguration.Mail.Port);
                        }
                        catch
                        {
                            ViewData["ErrorMessage"] = "An error occurred. Please try again";
                        }

                        ViewData["returnUrl"] = returnUrl;
                        return View("PasswordEmailed");
                    }
                    else
                    {
                        //no matching email
                        ViewData["ErrorMessage"] = "Email provided does not match any of our records";
                    }
                }
                else
                {
                    //invalid email
                    ViewData["ErrorMessage"] = "Email format is not valid";
                }
            }
            else
            {
                //no email
                ViewData["ErrorMessage"] = "Email is required";
            }

            return View();
        }