public ActionResult ChangePassword(ChangePasswordFormModel form) { if (ModelState.IsValid) { FNHMVCUser user = HttpContext.User.GetFNHMVCUser(); var command = new ChangePasswordCommand { UserId = user.UserId, OldPassword = form.OldPassword, NewPassword = form.NewPassword }; IEnumerable<ValidationResult> errors = commandBus.Validate(command); ModelState.AddModelErrors(errors); if (ModelState.IsValid) { var result = commandBus.Submit(command); if (result.Success) { return RedirectToAction("ChangePasswordSuccess"); } else { ModelState.AddModelError("", "The current password is incorrect or the new password is invalid."); } } } // If we got this far, something failed, redisplay form return View(form); }
public void UserChangePasswordTest() { using (var lifetime = container.BeginLifetimeScope()) { IUserRepository userRepository = lifetime.Resolve<IUserRepository>(); DefaultCommandBus commandBus = lifetime.Resolve<DefaultCommandBus>(); User user = userRepository.Get(c => c.Email == "*****@*****.**"); Assert.IsNotNull(user, "Error: User was not found"); ChangePasswordCommand command = new ChangePasswordCommand(); command.UserId = user.UserId; command.OldPassword = "******"; command.NewPassword = "******"; IValidationHandler<ChangePasswordCommand> validationHandler = lifetime.Resolve<IValidationHandler<ChangePasswordCommand>>(); IEnumerable<ValidationResult> validations = commandBus.Validate(command, validationHandler); foreach (var val in validations) { Assert.IsNull(val, "Error: User password change did not validate " + val.Message); } ICommandHandler<ChangePasswordCommand> commnadHandler = lifetime.Resolve<ICommandHandler<ChangePasswordCommand>>(); ICommandResult result = commandBus.Submit(command, commnadHandler); Assert.IsNotNull(result, "Error: User password change did not work"); Assert.IsTrue(result.Success, "Error: User password change did not work"); } }