Пример #1
0
        public async Task <ApiResponse> ResetPassword(ResetPasswordViewModel parameters)
        {
            var user = await _userManager.FindByIdAsync(parameters.UserId);

            if (user == null)
            {
                _logger.LogInformation(L["The user {0} doesn't exist", parameters.UserId]);
                return(new ApiResponse(Status404NotFound, L["The user doesn't exist"]));
            }

            // TODO: Break this out into it's own self-contained Email Helper service.

            var result = await _userManager.ResetPasswordAsync(user, parameters.Token, parameters.Password);

            if (result.Succeeded)
            {
                #region Email Successful Password change

                var email = _emailFactory.BuildPasswordResetEmail(user.UserName);
                email.ToAddresses.Add(new EmailAddressDto(user.Email, user.Email));

                _logger.LogInformation($"Reset Password Successful Email Sent: {user.Email}");
                await _emailManager.SendEmailAsync(email);

                #endregion Email Successful Password change

                return(new ApiResponse(Status200OK, $"Reset Password Successful Email Sent: {user.Email}"));
            }
            else
            {
                var msg = string.Join(",", result.Errors.Select(i => i.Description));
                _logger.LogWarning("Error while resetting the password: {0}", msg);
                return(new ApiResponse(Status400BadRequest, msg));
            }
        }
Пример #2
0
        public async Task <ApiResponse> ResetPassword(ResetPasswordViewModel parameters)
        {
            var user = await _userManager.FindByIdAsync(parameters.UserId);

            if (user == null)
            {
                _logger.LogInformation(L["The user {0} doesn't exist", parameters.UserId]);
                return(new ApiResponse(Status404NotFound, L["The user doesn't exist"]));
            }

            var result = await _userManager.ResetPasswordAsync(user, parameters.Token, parameters.Password);

            if (result.Succeeded)
            {
                var email = _emailFactory.BuildPasswordResetEmail(user.UserName);
                email.ToAddresses.Add(new EmailAddressDto(user.Email, user.Email));

                var response = await _emailManager.SendEmailAsync(email);

                if (response.IsSuccessStatusCode)
                {
                    _logger.LogInformation($"Reset Password Successful Email to {user.Email}");
                }
                else
                {
                    _logger.LogError($"Fail to send Reset Password Email to {user.Email}");
                }

                return(response);
            }
            else
            {
                var msg = string.Join(",", result.Errors.Select(i => i.Description));
                _logger.LogWarning("Error while resetting the password: {0}", msg);
                return(new ApiResponse(Status400BadRequest, msg));
            }
        }