コード例 #1
0
        public async Task <IActionResult> EditPassword(ChangePassword10 change)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    throw new Exception(ModelState.ToString());
                }

                var result = await _service.ChangePassword(change);

                if (string.IsNullOrWhiteSpace(result.Failed))
                {
                    return(Ok(result));
                }
                else if (result.Failed.Contains("found"))
                {
                    return(NotFound(result.Failed));
                }
                else
                {
                    throw new Exception(result.Failed);
                }
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }
コード例 #2
0
        public async Task <UserFrontUpdateVM> ChangePassword(ChangePassword10 changePassword)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(changePassword.NewPassword) ||
                    string.IsNullOrWhiteSpace(changePassword.OldPassword) ||
                    string.IsNullOrWhiteSpace(changePassword.ComparePassword))
                {
                    throw new Exception("One or more fields were empty.");
                }

                if (string.IsNullOrWhiteSpace(changePassword.UserId) || string.IsNullOrWhiteSpace(changePassword.UserToken))
                {
                    throw new Exception("Something went wrong.");
                }

                if (changePassword.NewPassword == changePassword.OldPassword)
                {
                    throw new Exception("The old password cannot be the same as the new one.");
                }
                else if (changePassword.NewPassword != changePassword.ComparePassword)
                {
                    throw new Exception("The passwords do not match.");
                }

                var user = await _userManager.FindByIdAsync(changePassword.UserId);

                if (user == null)
                {
                    throw new Exception("Cannot find the active user");
                }

                var userResult = await _userManager.VerifyUserTokenAsync(user, "Default", "authentication-backend", changePassword.UserToken);

                if (!userResult)
                {
                    throw new Exception("Cannot verify the active user.");
                }

                var result = await _userManager.ChangePasswordAsync(user, changePassword.OldPassword, changePassword.NewPassword);

                if (result.Succeeded)
                {
                    return(new UserFrontUpdateVM()
                    {
                        UserId = user.Id,
                        Success = "The password was successfully changed.",
                        FrontEndToken = VerificationToken(),
                        UserToken = await UserToken(user)
                    });
                }
                else
                {
                    throw new Exception("Incorrect password. Please try again.");
                }
            }
            catch (Exception ex)
            {
                return(new UserFrontUpdateVM()
                {
                    Failed = ex.Message
                });
            }
        }