コード例 #1
0
        public void ChangePassword_ChangesPassword()
        {
            securitySettings.AllowLoginAfterAccountCreation = true;
            securitySettings.RequireAccountVerification     = false;
            var id = subject.CreateAccount("test", "pass", "*****@*****.**").ID;

            subject.ChangePassword(id, "pass", "pass2");

            Assert.IsFalse(subject.Authenticate("test", "pass"));
            Assert.IsTrue(subject.Authenticate("test", "pass2"));
        }
コード例 #2
0
 public ActionResult ChangePassword(ChangePassword cp)
 {
     if (ModelState.IsValid)
     {
         using (UserAccountService _uas = new UserAccountService())
         {
             int        code = 0;
             User       user = _uas.GetUser(Convert.ToInt32(Session["Id"]));
             AesEncrpyt en   = new AesEncrpyt();
             if (user.Password.Equals(en.Encrypt(cp.OldPassword)))
             {
                 code = _uas.ChangePassword(cp, user);
                 if (code == 1)
                 {
                     ViewBag.ChangePasswordMessage = "Successfuly changed password!";
                 }
                 else
                 {
                     ViewBag.ChangePasswordMessage = "Password change unsuccessful.";
                 }
             }
             else
             {
                 ViewBag.ChangePasswordMessage = "Password change unsuccessful.";
             }
         }
     }
     return(View());
 }
コード例 #3
0
        public IActionResult ChangePassword([Bind(Prefix = "Password")] ChangePasswordInputModel model)
        {
            if (!ModelState.IsValid)
            {
                return(CorrectErrors(BuildUserProfileModel(model), "password"));
            }

            try
            {
                var acct = _userAccountService.GetByID(User.GetId());
                _userAccountService.ChangePassword(acct.ID, model.OldPassword, model.NewPassword);
                return(RedirectToAction("Edit", new { changed = true }));
            }
            catch (AuthenticationException)
            {
                return(new HttpUnauthorizedResult());
            }
            catch (ValidationException ex) {
                // this is more fragile than I'd like, but I don't see another way to check this exception's cause
                if (ex.ValidationResult.ErrorMessage == "Invalid old password.")
                {
                    ModelState.AddModelError("Password.OldPassword", ex.Message);
                }
                else
                {
                    ModelState.AddModelError("Password.NewPassword", ex.Message);
                    ModelState.AddModelError("Password.NewPasswordConfirm", ex.Message);
                }
            }

            return(CorrectErrors(BuildUserProfileModel(model), "password"));
        }
コード例 #4
0
        public void ChangePassword(ChangePasswordRequest request)
        {
            var userAccountRepository = new UserAccountRepository();

            UserAccountService.ChangePassword(
                request,
                ContextualUserId,
                userAccountRepository.GetById,
                userAccountRepository.SetPassword);
        }
コード例 #5
0
 public static void ChangePassword(ChangePasswordRequest request,
                                   int userId,
                                   Func <int, UserAccount> getUserAccountById,
                                   Action <string, int> setPasswordForUserId)
 {
     UserAccountService.ChangePassword(request.OldPassword,
                                       request.NewPassword,
                                       userId,
                                       getUserAccountById,
                                       setPasswordForUserId);
 }
コード例 #6
0
        public IActionResult ChangePassword(ChangePasswordViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }
            var user = GetCurrentUser();

            if (user != null)
            {
                _userAccountService.ChangePassword(user.ID, model.OldPassword, model.NewPassword);
                _authenticationService.SignIn(user, false);
                //_logger.LogInformation(3, "User changed their password successfully.");
                return(RedirectToAction(nameof(Index), new { Message = ManageMessageId.ChangePasswordSuccess }));
            }
            return(RedirectToAction(nameof(Index), new { Message = ManageMessageId.Error }));
        }
コード例 #7
0
        public void If_old_password_does_not_match_Then_throws_BadOldPasswordError()
        {
            var userId = 1234;

            Assert.Throws <InternalException>(() =>
                                              UserAccountService.ChangePassword(
                                                  new ChangePasswordRequest
            {
                OldPassword = "******"
            },
                                                  userId,
                                                  _ =>
                                                  new UserAccount
            {
                Password = "******",
                Enabled  = true
            },
                                                  (password, id) => { return; }));
        }
コード例 #8
0
        /// <summary>
        /// Changes user's password
        /// </summary>
        /// <typeparam name="TAccount"></typeparam>
        /// <param name="userAccountService"></param>
        /// <param name="newPass"></param>
        /// <param name="oldPass"></param>
        /// <returns></returns>
        public static async Task <ChangePassOutput> ChangePasswordAsync <TAccount>(
            UserAccountService <TAccount> userAccountService, string newPass, string oldPass)
            where TAccount : RelationalUserAccount
        {
            var output = new ChangePassOutput {
                Success = true
            };

            //need to verify the user pass first and in order to do so, need to simulate user auth
            var uuid = Utils.Identity.GetUserGuid();

            if (!uuid.HasValue)
            //this shouldn't happen really as the service should only allow authenticated access, but...
            {
                output.Success       = false;
                output.FailureReason = "unknown_user";
            }
            else
            {
                try
                {
                    userAccountService.ChangePassword(uuid.Value, oldPass, newPass);
                }
                catch (Exception ex)
                {
                    output.Success = false;

                    if (ex.Message == "Invalid old password.")
                    {
                        output.FailureReason = "invalid_old_pass";
                    }
                    if (ex.Message == "The new password must be different from the old password.")
                    {
                        output.FailureReason = "new_pass_same_as_old_pass";
                    }
                }
            }

            return(output);
        }