Exemplo n.º 1
0
        public async Task <ApiResponse> ForgotPassword(ForgotPasswordDto parameters)
        {
            if (!ModelState.IsValid)
            {
                return(new ApiResponse(400, "User Model is Invalid"));
            }

            var user = await _userManager.FindByEmailAsync(parameters.Email);

            if (user == null || !(await _userManager.IsEmailConfirmedAsync(user)))
            {
                _logger.LogInformation("Forgot Password with non-existent email / user: {0}", parameters.Email);
                // Don't reveal that the user does not exist or is not confirmed
                return(new ApiResponse(200, "Success"));
            }

            #region Forgot Password Email

            try
            {
                // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=532713
                var token = await _userManager.GeneratePasswordResetTokenAsync(user);

                string callbackUrl = string.Format("{0}/Account/ResetPassword/{1}?token={2}", _configuration["BlazorBoilerplate:ApplicationUrl"], user.Id, token); //token must be a query string parameter as it is very long

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

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

                return(new ApiResponse(200, "Forgot Password Email Sent"));
            }
            catch (Exception ex)
            {
                _logger.LogInformation("Forgot Password email failed: {0}", ex.Message);
            }

            #endregion Forgot Password Email

            return(new ApiResponse(200, "Success"));
        }
Exemplo n.º 2
0
        public async Task <ApiResponse> ForgotPassword(ForgotPasswordDto parameters)
        {
            var user = await _userManager.FindByEmailAsync(parameters.Email);

            if (user == null || !(await _userManager.IsEmailConfirmedAsync(user)))
            {
                _logger.LogInformation("Такого пользователя/почты не существует: {0}", parameters.Email);
                // Don't reveal that the user does not exist or is not confirmed
                return(new ApiResponse(Status200OK, "Успех"));
            }

            // TODO: Break out the email sending here, to a separate class/service etc..
            #region Forgot Password Email

            try
            {
                // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=532713
                var token = await _userManager.GeneratePasswordResetTokenAsync(user);

                string callbackUrl = string.Format("{0}/Account/ResetPassword/{1}?token={2}", _configuration["BlazorBoilerplate:ApplicationUrl"], user.Id, token); //token must be a query string parameter as it is very long

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

                _logger.LogInformation("Отправлено письмо на почту: {0}", user.Email);
                await _emailManager.SendEmailAsync(email);

                return(new ApiResponse(Status200OK, "Отправлено письмо на почту"));
            }
            catch (Exception ex)
            {
                _logger.LogInformation("Отправка письма провалилась: {0}", ex.Message);
            }

            #endregion Forgot Password Email

            return(new ApiResponse(Status200OK, "Успех"));
        }