Example #1
0
        public async Task SendChangePasswordEmail([FromBody] SendChangePasswordRequest data)
        {
            var user = await _userService.GetByEmailAsync(data.Email);

            if (user == null)
            {
                throw new AppErrorException(new AppError(ErrorCode.IncorrectData));
            }

            var accessToken  = _authService.GenerateAccessToken(user);
            var refreshToken = _authService.GenerateRefreshToken(user);

            var model = new ChangePasswordModel {
                AccessToken = accessToken, RereshToken = refreshToken, Email = user.Email
            };

            await _queueProvider.SendMessageAsync(model.ToJson());
        }
    public async Task <ActionResult> ChangePassword([FromBody] ChangePasswordModel model)
    {
        var userId = User.GetUserId();
        var user   = await userMgr.FindByIdAsync(userId);

        if (user == null)
        {
            return(Forbid());
        }
        try {
            model.CurrentPassword = Base64UrlEncoder.Decode(model.CurrentPassword);
            model.NewPassword     = Base64UrlEncoder.Decode(model.NewPassword);
            model.ConfirmPassword = Base64UrlEncoder.Decode(model.ConfirmPassword);
            var isValid = await userMgr.CheckPasswordAsync(user, model.CurrentPassword);

            if (!isValid)
            {
                return(BadRequest("Invalid current password!"));
            }
            var result = await userMgr.ChangePasswordAsync(user, model.CurrentPassword, model.NewPassword);

            if (result.Succeeded)
            {
                return(Ok());
            }
            return(BadRequest(result.Errors));
        }
        catch (Exception ex) {
            logger.LogError(ex, $"Can not change password for user {user.UserName} with {model.ToJson()} .");
            return(this.InternalServerError(ex));
        }
    }