masksoverflow
masksoverflow

Reputation: 49

Email reset password not working, however account confirmation works .NetCore

So I followed the instructions here: https://learn.microsoft.com/en-us/aspnet/core/security/authentication/accconfirm?view=aspnetcore-3.1&tabs=visual-studio

My application works and sends out an account confirmation email using the Email service I set up.

However if I go to reset password, the identity service does not call the email service to send the email instructions. What am I doing wrong?

How can the identity service know how to use email to confirm account but can't use it to reset password?

For context

 startup.cs


 // requires
 services.AddTransient<IEmailSender, EmailSender>();
 services.Configure<AuthMessageSenderOptions>(Configuration);


 EmailSender.cs

public class EmailSender:IEmailSender
{
    public EmailSender(IOptions<AuthMessageSenderOptions> optionsAccessor)
    {
        Options = optionsAccessor.Value;
    }

    public AuthMessageSenderOptions Options { get; } //set only via Secret Manager

    public Task SendEmailAsync(string email, string subject, string message)
    {
        return Execute(Options.SendGridKey, subject, message, email);
    }

    public Task Execute(string apiKey, string subject, string message, string email)
    {
        var client = new SendGridClient(apiKey);
        var msg = new SendGridMessage()
        {
            From = new EmailAddress("[email protected]", Options.SendGridUser),
            Subject = subject,
            PlainTextContent = message,
            HtmlContent = message
        };
        msg.AddTo(new EmailAddress(email));

        // Disable click tracking.
        // See https://sendgrid.com/docs/User_Guide/Settings/tracking.html
        msg.SetClickTracking(false, false);

        return client.SendEmailAsync(msg);
    }
}

Upvotes: 0

Views: 987

Answers (1)

Kurtbaby
Kurtbaby

Reputation: 846

We ran into the same issue and discovered that while registration emails were being sent, password reset emails were not. In our case, it was because of two issues with the AspNetUsers table:

  1. We didn't complete the email confirmation step, which sets the EmailConfirmed bool in the database to True, and
  2. We had migrated the data over from an earlier version of ASP.NET Core where the two email fields, Email and NormalizedEmail were not set and were left null.

Once we populated the missing email fields, and ran the confirmation step—or manually changed the flag to True—the password reset emails started working.

Upvotes: 3

Related Questions