Esempio n. 1
0
        public async Task <IActionResult> ChangePassword([FromBody] ChangePasswordResource changePwd)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var user = await _userManager.FindByIdAsync(changePwd.Id);

            if (user == null)
            {
                return(BadRequest(("find_user_failure", "Не удалость найти пользователя.", ModelState)));
            }

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

            if (result.Succeeded)
            {
                return(new OkObjectResult("Password changed."));
            }
            else
            {
                if (result.Errors.Any())
                {
                    return(BadRequest(("change_password_failure", "Старый пароль введен не верно")));
                }
                // return BadRequest(result.Errors.ToArray());
            }

            return(new OkObjectResult("Password changed."));
        }
Esempio n. 2
0
        // [Authorize(AuthenticationSchemes = "Bearer", Policy = "ApiUser", Roles = "Администратор")]
        public async Task <IActionResult> ChangePasswordHash([FromBody] ChangePasswordResource pwd)
        {
            var user = await _userManager.FindByIdAsync(pwd.Id);

            if (user == null)
            {
                return(BadRequest(("find_user_failure", "Не удалость найти пользователя.", ModelState)));
            }

            var _passwordValidator =
                HttpContext.RequestServices.GetService(typeof(IPasswordValidator <AppUser>)) as IPasswordValidator <AppUser>;
            var _passwordHasher =
                HttpContext.RequestServices.GetService(typeof(IPasswordHasher <AppUser>)) as IPasswordHasher <AppUser>;

            IdentityResult result =
                await _passwordValidator.ValidateAsync(_userManager, user, pwd.NewPassword);

            if (result.Succeeded)
            {
                user.PasswordHash = _passwordHasher.HashPassword(user, pwd.NewPassword);
                await _userManager.UpdateAsync(user);

                return(new OkObjectResult("Password changed."));
            }
            else
            {
                if (result.Errors.Any())
                {
                    return(BadRequest(("change_password_failure", result.Errors.ToArray())));
                }
            }

            return(new OkObjectResult("Password changed."));
        }
Esempio n. 3
0
        public async Task <IActionResult> ChangePassword([FromBody] ChangePasswordResource changePasswordResource)
        {
            var result = await _authService.ChangePasswordAsync(User.GetUserId(), changePasswordResource);

            if (!result.Succeeded)
            {
                return(BadRequest(result.ErrorMessage));
            }

            return(Ok(result.Data));
        }
Esempio n. 4
0
        public async Task <IActionResult> ChangePassword([FromBody] ChangePasswordResource resource)
        {
            var userId = User.GetUserId();
            var result = await _authService.ChangePasswordAsync(userId, resource.OldPassword, resource.NewPassword);

            if (!result.Succeeded)
            {
                return(BadRequest(_response.Error(result.Message)));
            }

            return(Ok(_response.Ok(result.Data)));
        }
Esempio n. 5
0
        public async Task <IActionResult> ChangePassword(ChangePasswordResource model)
        {
            var user = await _usersService.GetCurrentUserAsync();

            if (user == null)
            {
                return(BadRequest("NotFound"));
            }

            var result = await _usersService.ChangePasswordAsync(user, model.OldPassword, model.NewPassword);

            if (result.Succeeded)
            {
                return(Ok());
            }

            return(BadRequest(result.Error));
        }
        public async Task <IActionResult> ChangePassword([FromBody] ChangePasswordResource changePasswordResource, UserResource userResource)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var userIdentity = _mapper.Map <ApplicationUser>(userResource);

            var result = await _userManager.ChangePasswordAsync(userIdentity, changePasswordResource.CurrentPassword, changePasswordResource.NewPassword);

            if (!result.Succeeded)
            {
                return(BadRequest(result.Errors));
            }

            return(Ok());
        }
Esempio n. 7
0
        public async Task <IActionResult> ChangePassword(ChangePasswordResource model)
        {
            var user = await _userManager.FindByEmailAsync(model.Email);

            if (user != null)
            {
                var result = await _userManager.ChangePasswordAsync(user, model.Token, model.Password);

                if (!result.Succeeded)
                {
                    return(BadRequest(new { message = "Cannot change password" }));
                }

                return(Ok(result));
            }
            else
            {
                return(BadRequest(new { message = "User not found" }));
            }
        }
        public async Task <IActionResult> ChangePassword([FromBody] ChangePasswordResource changePasswordResource)
        {
            var email = User.FindFirst("Email").Value;

            var account = await unitOfWork.UserAccountRepository.GetUserAccountAsync(email);

            if (account == null || account.IsDeleted)
            {
                return(NotFound("Account could not be found."));
            }

            // Verify email address has valid structure
            string normalizedAddress;

            if (!EmailExtension.TryNormalizeEmail(email, out normalizedAddress))
            {
                return(BadRequest("Not a valid email address!"));
            }

            var  password      = changePasswordResource.OldPassword + account.Salt;
            bool validPassword = BCrypt.Net.BCrypt.Verify(password, account.Password);

            if (validPassword)
            {
                // Salt and hash password
                var newSalt           = BCrypt.Net.BCrypt.GenerateSalt();
                var newSaltedPassword = changePasswordResource.NewPassword + newSalt;
                var newHashedPassword = BCrypt.Net.BCrypt.HashPassword(newSaltedPassword);

                account.Password = newHashedPassword;
                account.Salt     = newSalt;
                Task.WaitAll(unitOfWork.CompleteAsync());
                return(Ok());
            }
            else
            {
                return(Unauthorized());
            }
        }
Esempio n. 9
0
        public async Task <IActionResult> ChangePassword([FromBody] ChangePasswordResource changePasswordResource)
        {
            var userEmail = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Email).Value;
            var user      = unitOfWork.Accounts.FindUserByEmail(userEmail);

            if (user == null)
            {
                return(NotFound(new ApiResponse(404, "User not found")));
            }

            var hashedPassword = user.Password;

            if (!SecurePasswordHasherHelper.Verify(changePasswordResource.OldPassword, hashedPassword))
            {
                return(BadRequest(new ApiResponse(400, "Enter correct old password")));
            }

            user.Password = SecurePasswordHasherHelper.Hash(changePasswordResource.NewPassword);
            await unitOfWork.CompleteAsync();

            return(Ok(new ApiResponse(200, "Password changed successfully")));
        }
Esempio n. 10
0
        public async Task <GenericResponse <StatusResponse> > ChangePasswordAsync(string userId, ChangePasswordResource resource)
        {
            var user = await _userManager.FindByIdAsync(userId);

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

            if (!result.Succeeded)
            {
                return new GenericResponse <StatusResponse>
                       {
                           Succeeded    = false,
                           ErrorMessage = string.Join(", ", result.Errors.Select(x => x.Description))
                       }
            }
            ;

            return(new GenericResponse <StatusResponse>
            {
                Succeeded = true,
                Data = new StatusResponse
                {
                    Status = "The account password has been updated"
                }
            });
        }