Exemplo n.º 1
0
        private async Task SendResetPasswordEmailAsync(User user, EmailPurposeEnum emailPurpose)
        {
            var resetPasswordToken = await _tokenProviderService.GeneratePasswordResetTokenAsync(user);

            var callbackUrl =
                UrlHelperExtensions.ApiCallbackLink(_authApiUrlConfiguration.ResetPasswordUrl, resetPasswordToken);

            string baseEmailTemplate;

            switch (emailPurpose)
            {
            case EmailPurposeEnum.ResetPassword:
                baseEmailTemplate = _emailTemplateProvider.GetBaseTemplateHtml();

                var resetPasswordEmailTemplate = _emailTemplateProvider.GetTemplateHtml(
                    _emailTemplatesConfiguration.ResetPasswordEmailTemplateName);

                await _emailService.SendNotificationAsync(new ResetPasswordEmailNotification(
                                                              new ResetPasswordEmailModel(callbackUrl, user.Email),
                                                              new RecipientDto(user.Email, user.UserName),
                                                              baseEmailTemplate,
                                                              resetPasswordEmailTemplate));

                break;

            case EmailPurposeEnum.PasswordExpiration:
                baseEmailTemplate = _emailTemplateProvider.GetBaseTemplateHtml();

                var passwordHasExpiredEmailTemplate = _emailTemplateProvider.GetTemplateHtml(
                    _emailTemplatesConfiguration.PasswordHasExpiredEmailTemplateName);

                if (!user.LastPasswordExpiredEmailSentDate.HasValue ||
                    user.LastPasswordExpiredEmailSentDate.Value.AddMinutes(_passwordConfiguration
                                                                           .TokenLifeTimeInMinutes) <= DateTimeOffset.UtcNow)
                {
                    await _emailService.SendNotificationAsync(new PasswordHasExpiredEmailNotification(
                                                                  new PasswordHasExpiredEmailModel(callbackUrl, user.Email),
                                                                  new RecipientDto(user.Email, user.UserName),
                                                                  baseEmailTemplate,
                                                                  passwordHasExpiredEmailTemplate));

                    await UpdateLastPasswordExpiredEmailSentDateAsync(user.Id);
                }

                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(emailPurpose), emailPurpose,
                                                      UnsupportedEmailPurposeException);
            }
        }
Exemplo n.º 2
0
        public async Task SendChangePasswordEmailAsync(User user)
        {
            var resetPasswordToken = await _tokenProviderService.GeneratePasswordResetTokenAsync(user);

            var callbackUrl = UrlHelperExtensions.ApiCallbackLink(_authApiUrlConfiguration.ResetPasswordUrl, resetPasswordToken);

            var baseEmailTemplate = _emailTemplateProvider.GetBaseTemplateHtml();

            var changePasswordEmailTemplate = _emailTemplateProvider.GetTemplateHtml(
                _emailTemplatesConfiguration.ChangePasswordEmailTemplateName);

            await _emailService.SendNotificationAsync(new ChangePasswordEmailNotification(
                                                          new ChangePasswordEmailModel(callbackUrl, user.Email),
                                                          new RecipientDto(user.Email, user.UserName),
                                                          baseEmailTemplate,
                                                          changePasswordEmailTemplate));
        }
Exemplo n.º 3
0
        public async Task SendEmailConfirmationEmailAsync(User user)
        {
            var token = await _tokenProviderService.GenerateEmailConfirmationTokenAsync(user);

            var callbackLink = UrlHelperExtensions.ApiCallbackLink(_authApiUrlConfiguration.ConfirmEmailUrl, token);

            var baseEmailTemplate = _emailTemplateProvider.GetBaseTemplateHtml();

            var emailConfirmationEmailTemplate = _emailTemplateProvider.GetTemplateHtml(
                _emailTemplatesConfiguration.EmailConfirmationEmailTemplateName);

            await _emailService.SendNotificationAsync(new UserConfirmationEmailNotification(
                                                          new UserConfirmationEmailModel(callbackLink, user.Email),
                                                          emailConfirmationEmailTemplate,
                                                          baseEmailTemplate,
                                                          new RecipientDto(user.Email, user.UserName)));
        }