示例#1
0
        public async Task <ApiResponse> ForgotPassword(ForgotPasswordViewModel parameters)
        {
            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(Status200OK, L["Operation Successful"]));
            }

            // 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}", baseUrl, user.Id, token); //token must be a query string parameter as it is very long

            var email = _emailFactory.BuildForgotPasswordEmail(user.UserName, callbackUrl, token);

            email.ToAddresses.Add(new EmailAddressDto(user.Email, user.Email));

            var response = await _emailManager.SendEmailAsync(email);

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

            return(response);
        }
示例#2
0
        public async Task <ApiResponse> ForgotPassword(ForgotPasswordViewModel parameters)
        {
            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(Status200OK, L["Operation Successful"]));
            }

            // 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 = _emailFactory.BuildForgotPasswordEmail(user.UserName, callbackUrl, token);
                email.ToAddresses.Add(new EmailAddressDto(user.Email, user.Email));

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

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

            #endregion Forgot Password Email

            return(new ApiResponse(Status200OK, L["Operation Successful"]));
        }