web-dev-qa-db-fra.com

Impossible de résoudre le service pour le type IEmailSender lors de la tentative d'activation de RegisterModel

J'utilise Identity et j'ai un problème que je crée un nouveau projet d'exemple et avec l'authentification individuelle et l'identité d'échafaudage InvalidOperationException: Impossible de résoudre le service pour le type 'Microsoft.AspNetCore.Identity.UI.Services.IEmailSender' tout en essayant d'activer ' MASQ.Areas.Identity.Pages.Account.RegisterModel '.

8
Muhammad Abdullah

Il y a deux façons de procéder:

  1. supprimez la services.AddDefaultTokenProviders() dans la ConfigurureServices() pour désactiver two-factor authentication (2FA):

// fichier: Startup.cs:

services.AddDefaultIdentity<IdentityUser>()
    .AddEntityFrameworkStores<ApplicationDbContext>();
    ///.AddDefaultTokenProviders(); /// remove this line
  1. Ajoutez vos propres implémentations IEmailSender et ISmsSender à DI contianer si vous souhaitez activer 2FA

// fichier: Startup.cs

services.AddTransient<IEmailSender,YourEmailSender>();
services.AddTransient<IEmailSender,YourSmsSender>();

Les deux devraient fonctionner.

8
itminus
public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<CookiePolicyOptions>(options =>
        {
            // This lambda determines whether user consent for non-essential cookies is needed for a given request.
            options.CheckConsentNeeded = context => true;
            options.MinimumSameSitePolicy = SameSiteMode.None;
        });
        services.AddDbContext<ApplicationDbContext>(options =>
        options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
        services.AddIdentity<ApplicationUser, ApplicationRole>(
           option => {
               option.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(5);
               option.Lockout.MaxFailedAccessAttempts = 5;
               option.Lockout.AllowedForNewUsers = false;
           })
          .AddEntityFrameworkStores<ApplicationDbContext>()
          .AddDefaultTokenProviders();

        //services.AddDbContext<ApplicationDbContext>(options =>
        //    options.UseSqlServer(
        //        Configuration.GetConnectionString("DefaultConnection")));
        //services.AddIdentity<ApplicationUser, IdentityRole>()
        //    .AddEntityFrameworkStores<ApplicationDbContext>().AddDefaultTokenProviders();

        services.AddTransient<Areas.Identity.Services.IEmailSender, AuthMessageSender>();

        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseDatabaseErrorPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseCookiePolicy();

        app.UseAuthentication();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });
    }
}
0
Muhammad Abdullah