尝试激活RegisterModel时无法解析类型IEmailSender的服务

问题描述 投票:5回答:2

我正在使用Identity,我遇到了一个问题,即我创建了一个新的示例项目并使用了个人身份验证和脚手架标识InvalidOperationException:尝试激活时无法解析类型为“Microsoft.AspNetCore.Identity.UI.Services.IEmailSender”的服务MASQ.Areas.Identity.Pages.Account.RegisterModel”。

asp.net-core asp.net-identity
2个回答
6
投票

有两种方法可以做到这一点:

  1. 删除services.AddDefaultTokenProviders()中的ConfigurureServices()以禁用two-factor authentication (2FA)

//文件:Startup.cs

services.AddDefaultIdentity<IdentityUser>()
    .AddEntityFrameworkStores<ApplicationDbContext>();
    ///.AddDefaultTokenProviders(); /// remove this line
  1. 如果你想启用IEmailSender,请将自己的ISmsSender2FA实现添加到DO容器中

//文件:Startup.cs

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

两者都应该有效。


0
投票
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?}");
        });
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.