public async Task <IActionResult> ChangeUserPassword(AdminChangeUserPassword10 changePassword)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    throw new Exception("Please check all fields and then try again.");
                }

                if (string.IsNullOrWhiteSpace(changePassword.AdminId) || string.IsNullOrWhiteSpace(changePassword.AdminToken))
                {
                    throw new Exception("Something went wrong!");
                }

                var result = await _service.ChangeUserPassword(changePassword);

                if (result.Failed == null)
                {
                    return(Ok(result));
                }

                throw new Exception(result.Failed);
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }
Ejemplo n.º 2
0
        public async Task <AdminResultVM> ChangeUserPassword(AdminChangeUserPassword10 changePassword)
        {
            try
            {
                if (
                    string.IsNullOrWhiteSpace(changePassword.OldPassword) || string.IsNullOrWhiteSpace(changePassword.NewPassword) ||
                    string.IsNullOrWhiteSpace(changePassword.ComparePassword))
                {
                    throw new Exception("One or more fields were empty.");
                }

                if (changePassword.NewPassword != changePassword.ComparePassword)
                {
                    throw new Exception("Passwords does not match");
                }

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

                var admin = await _userManager.FindByIdAsync(changePassword.AdminId);

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

                var adminResult = await _userManager.VerifyUserTokenAsync(admin, "Default", "authentication-backend", changePassword.AdminToken);

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

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

                if (user == null)
                {
                    throw new Exception("User could not be found.");
                }

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

                if (result.Succeeded)
                {
                    return(new AdminResultVM()
                    {
                        AdminId = admin.Id,
                        Success = "Password was successfully updated!",
                        FrontEndToken = VerificationToken(),
                        AdminToken = await UserToken(admin),
                    });
                }
                else
                {
                    throw new Exception(result.Errors.ToString());
                }
            }
            catch (Exception ex)
            {
                return(new AdminResultVM()
                {
                    Failed = ex.Message
                });
            }
        }