Пример #1
0
        public async Task <ApiResponse> ResetPassword(ResetPasswordDto parameters)
        {
            if (!ModelState.IsValid)
            {
                return(new ApiResponse(400, "User Model is Invalid"));
            }

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

            if (user == null)
            {
                _logger.LogInformation("User does not exist: {0}", parameters.UserId);
                return(new ApiResponse(404, "User does not exist"));
            }

            #region Reset Password Successful Email

            try
            {
                IdentityResult result = await _userManager.ResetPasswordAsync(user, parameters.Token, parameters.Password);

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

                    var email = new EmailMessageDto();
                    email.ToAddresses.Add(new EmailAddressDto(user.Email, user.Email));
                    email.BuildPasswordResetEmail(user.UserName); //Replace First UserName with Name if you want to add name to Registration Form

                    _logger.LogInformation("Reset Password Successful Email Sent: {0}", user.Email);
                    await _emailService.SendEmailAsync(email);

                    #endregion Email Successful Password change

                    return(new ApiResponse(200, String.Format("Reset Password Successful Email Sent: {0}", user.Email)));
                }
                else
                {
                    _logger.LogInformation("Error while resetting the password!: {0}", user.UserName);
                    return(new ApiResponse(400, string.Format("Error while resetting the password!: {0}", user.UserName)));
                }
            }
            catch (Exception ex)
            {
                _logger.LogInformation("Reset Password failed: {0}", ex.Message);
                return(new ApiResponse(400, string.Format("Error while resetting the password!: {0}", ex.Message)));
            }

            #endregion Reset Password Successful Email
        }
Пример #2
0
        public async Task <ApiResponse> ResetPassword(ResetPasswordDto parameters)
        {
            var user = await _userManager.FindByIdAsync(parameters.UserId);

            if (user == null)
            {
                _logger.LogInformation("Пользователя не существует: {0}", parameters.UserId);
                return(new ApiResponse(Status404NotFound, "Пользователя не существует"));
            }

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

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

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

                    var email = new EmailMessageDto();
                    email.ToAddresses.Add(new EmailAddressDto(user.Email, user.Email));
                    email.BuildPasswordResetEmail(user.UserName); //Replace First UserName with Name if you want to add name to Registration Form

                    _logger.LogInformation($"Письмо на изменения пароля отправлено: {user.Email}");
                    await _emailManager.SendEmailAsync(email);

                    #endregion Email Successful Password change

                    return(new ApiResponse(Status200OK, $"Отправлено письмо на изменение пароля: {user.Email}"));
                }
                else
                {
                    _logger.LogInformation($"Ошибка при изменении пароля!: {user.UserName}");
                    return(new ApiResponse(Status400BadRequest, $"Ошибка при изменении пароля!: {user.UserName}"));
                }
            }
            catch (Exception ex)
            {
                _logger.LogInformation($"Не удалось изменить пароль: {ex.Message}");
                return(new ApiResponse(Status400BadRequest, $"Ошибка при изменении пароля!: {ex.Message}"));
            }
        }
Пример #3
0
        public async Task <ApiResponse> ResetPassword(ResetPasswordDto parameters)
        {
            var user = await _userManager.FindByIdAsync(parameters.UserId);

            if (user == null)
            {
                _logger.LogInformation("User does not exist: {0}", parameters.UserId);
                return(new ApiResponse(Status404NotFound, "User does not exist"));
            }

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

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

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

                    var email = new EmailMessageDto();
                    email.ToAddresses.Add(new EmailAddressDto(user.Email, user.Email));
                    email.BuildPasswordResetEmail(user.UserName); //Replace First UserName with Name if you want to add name to Registration Form

                    _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
                {
                    _logger.LogInformation($"Error while resetting the password!: {user.UserName}");
                    return(new ApiResponse(Status400BadRequest, $"Error while resetting the password!: {user.UserName}"));
                }
            }
            catch (Exception ex)
            {
                _logger.LogInformation($"Reset Password failed: {ex.Message}");
                return(new ApiResponse(Status400BadRequest, $"Error while resetting the password!: {ex.Message}"));
            }
        }
Пример #4
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 = new EmailMessageDto();
                email.ToAddresses.Add(new EmailAddressDto(user.Email, user.Email));
                email.BuildPasswordResetEmail(user.UserName); //Replace First UserName with Name if you want to add name to Registration Form

                _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));
            }
        }