Esempio n. 1
0
        public async Task <IActionResult> ResetPassword([FromBody] ResetPasswordConfirmDto resetPasswordDto)
        {
            try
            {
                new PasswordValidator(resetPasswordDto.Password, resetPasswordDto.ConfirmPassword).Validate();
                await _service.ResetPasswordAsync(resetPasswordDto);

                return(Ok());
            }
            catch (ObjectNotFoundException ex)
            {
                return(BadRequest(ex.ToString()));
            }
            catch (ObjectUpdateException ex)
            {
                return(BadRequest(ex.ToString()));
            }
            catch (ArgumentException ex)
            {
                return(BadRequest(ex.Message));
            }
            catch (ValidationException ex)
            {
                return(BadRequest(ex.ToString()));
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }
Esempio n. 2
0
        public async Task ResetPasswordAsync(ResetPasswordConfirmDto model)
        {
            var user = await _userManager.FindByIdAsync(model.UserId);

            if (user == null)
            {
                throw new ObjectNotFoundException(ExceptionStrings.UserNotFoundException, model.UserId);
            }

            var decodedToken = model.Token.Base64UrlDecodeString();

            var resetPassResult = await _userManager.ResetPasswordAsync(user, decodedToken, model.Password);

            if (!resetPassResult.Succeeded)
            {
                throw new ObjectUpdateException(ExceptionStrings.ResetPasswordException + string.Join(" ", resetPassResult.Errors.ToListOfStrings()), user.Email);
            }
        }
Esempio n. 3
0
        public async Task <IActionResult> ResetPasswordConfirm([FromBody] ResetPasswordConfirmDto resetPasswordDto)
        {
            async Task ResetPassword()
            {
                bool PasswordsDoNotMatch()
                {
                    return(resetPasswordDto.NewPassword != null && resetPasswordDto.NewPassword != resetPasswordDto.ConfirmPassword);
                }

                if (PasswordsDoNotMatch())
                {
                    throw new FoodsValidationException("Password", "", "The new password and the confirmed password do not match.");
                }

                var user = await _userManager.FindByIdAsync(resetPasswordDto.IdentityId);

                bool HasEnoughDetailsToResetPassword()
                {
                    return(user != null && !string.IsNullOrEmpty(resetPasswordDto.NewPassword) && !string.IsNullOrEmpty(resetPasswordDto.ConfirmPassword));
                }

                if (!HasEnoughDetailsToResetPassword())
                {
                    throw new FoodsValidationException("Email", "",
                                                       "There was an issuing when resetting the password. The password has not been changed");
                }

                var result = await _userManager.ResetPasswordAsync(user, resetPasswordDto.ResetToken, resetPasswordDto.NewPassword);

                CheckIdentityResult(result, "Username/Password");

                await _accountHelper.SendResetPasswordSuccessEmail(user);
            }

            return(await Execute(ResetPassword));
        }