Unable to resolve service for type IEmailSender while attempting to activate RegisterModel

I am using ASP.NET Core 3.0 and had similar issue. I added the following .AddDefaultUI() to my Startup.cs & it worked.

 public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlServer(
                    Configuration.GetConnectionString("DefaultConnection")));
            services.AddIdentity<IdentityUser, IdentityRole>()
                .AddDefaultTokenProviders()
                .AddDefaultUI()
                .AddEntityFrameworkStores<ApplicationDbContext>();

            services.AddControllersWithViews();
            services.AddRazorPages().AddRazorRuntimeCompilation();
        }

There're two ways to do that :

  1. remove the services.AddDefaultTokenProviders() in the ConfigurureServices() to disable two-factor authentication (2FA) :
    // file: `Startup.cs` :
    services.AddDefaultIdentity<IdentityUser>()
        .AddEntityFrameworkStores<ApplicationDbContext>();
        ///.AddDefaultTokenProviders(); /// remove this line
    
  2. Add your own IEmailSender and ISmsSender implementation to DI contianer if you would like to enable 2FA

    // file: `Startup.cs`
    
    services.AddTransient<IEmailSender,YourEmailSender>();
    services.AddTransient<IEmailSender,YourSmsSender>();
    

Edit:

Both should work.

Both should work for ASP.NET Core 2.1. However, as of ASP.NET Core 3.0, the first approach doesn't work any more.