public ActionResult Index(ChangePasswordViewModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    model.Validate();

                    membershipService.ChangePasswordForLoggedInUser(model.CurrentPassword, model.NewPassword);
                    membershipService.Commit();

                    SuccessMessage(FormMessages.AccountChangePassword);
                    return(RedirectToAction("Index", "MatchResult"));
                }
                catch (ChangePasswordNewPasswordsDoNotMatchException)
                {
                    ErrorMessage(FormMessages.AccountChangePasswordNewAndConfirmPasswordsDoNotMatch);
                }
                catch (ChangePasswordCurrentPasswordIncorrectException)
                {
                    ErrorMessage(FormMessages.AccountChangePasswordCurrentPasswordIncorrect);
                }
            }

            return(View(model));
        }
Esempio n. 2
0
        public ActionResult ChangePassword(ChangePasswordViewModel form)
        {
            try
            {
                // 0. Validate user input
                form.Validate();

                // 1. Get data from User
                using (var userRepo = new UserRepository())
                {
                    var user = userRepo.Get()
                               .Where(
                        u =>
                        u.UserName == form.Username &&
                        u.Password == form.Password)
                               .FirstOrDefault();

                    if (user == null)
                    {
                        throw new Exception("รหัสผ่านเดิมไม่ถูกต้อง");
                    }

                    // 2. Change Password
                    user.Password = form.NewPassword;
                    userRepo.Update(user);

                    // 3. Return
                    returnobj.SetSuccess("เปลี่ยนรหัสผ่านสำเร็จ");
                }
            }
            catch (Exception ex)
            {
                returnobj.SetError(ex.Message);
            }
            return(Content(returnobj.ToJson(), "application/json"));
        }