Example #1
0
        public async Task <IActionResult> SetPassword([FromBody] SetPasswordRequestModel model)
        {
            if (ModelState.IsValid)
            {
                SetPasswordActionResult result = await _accountService.SetPasswordActionAsync(model.CurrentPassword, model.NewPassword);

                if (result == SetPasswordActionResult.NoLoggedInAccount)
                {
                    // Logged in but unable to retrieve account
                    throw new Exception();
                }

                if (result == SetPasswordActionResult.Success)
                {
                    return(Ok());
                }

                if (result == SetPasswordActionResult.AlreadySet)
                {
                    ModelState.AddModelError(nameof(SetPasswordRequestModel.NewPassword), Strings.ErrorMessage_NewPassword_MustDiffer);
                }
                else if (result == SetPasswordActionResult.InvalidCurrentPassword)
                {
                    ModelState.AddModelError(nameof(SetPasswordRequestModel.CurrentPassword), Strings.ErrorMessage_Password_Invalid);
                }
            }

            return(BadRequest(new SetPasswordResponseModel
            {
                ExpectedError = true,
                ModelState = new SerializableError(ModelState)
            }));
        }
        public bool UpdatePassword(SetPasswordRequestModel input)
        {
            var user = this.RepositoryContext.User.Where(x => x.UserId == input.UserId).FirstOrDefault();

            if (user != null)
            {
                user.Password     = input.NewPassword;
                user.ModifiedBy   = input.LoginUserId;
                user.ModifiedDate = DateTime.Now;
            }
            this.RepositoryContext.Update(user);

            return(true);
        }
Example #3
0
        public async Task PostSetPasswordAsync([FromBody] SetPasswordRequestModel model)
        {
            var user = await _userService.GetUserByPrincipalAsync(User);

            if (user == null)
            {
                throw new UnauthorizedAccessException();
            }

            var result = await _userService.SetPasswordAsync(model.ToUser(user), model.MasterPasswordHash, model.Key);

            if (result.Succeeded)
            {
                return;
            }

            foreach (var error in result.Errors)
            {
                ModelState.AddModelError(string.Empty, error.Description);
            }

            throw new BadRequestException(ModelState);
        }