C# 无法解析类型为';Microsoft.AspNetCore.Identity.UserManager`试图激活';AuthController';

C# 无法解析类型为';Microsoft.AspNetCore.Identity.UserManager`试图激活';AuthController';,c#,asp.net,asp.net-mvc,asp.net-core,asp.net-core-identity,C#,Asp.net,Asp.net Mvc,Asp.net Core,Asp.net Core Identity,我在登录控制器中遇到此错误 InvalidOperationException:在尝试激活“Automobile.Server.Controllers.AuthController”时,无法解析类型“Microsoft.AspNetCore.Identity.UserManager”“1[Automobile.Models.Account]”的服务 以下是身份验证控制器构造函数: private SignInManager<Automobile.Models.Account> _si

我在登录控制器中遇到此错误

InvalidOperationException:在尝试激活“Automobile.Server.Controllers.AuthController”时,无法解析类型“Microsoft.AspNetCore.Identity.UserManager”“1[Automobile.Models.Account]”的服务

以下是身份验证控制器构造函数:

private SignInManager<Automobile.Models.Account> _signManager;
    private UserManager<Automobile.Models.Account> _userManager;

    public AuthController(UserManager<Models.Account> userManager,
                          SignInManager<Automobile.Models.Account> signManager)
    {
        this._userManager = userManager;
        this._signManager = signManager;
    }
private SignInManager\u signManager;
私人用户管理器(UserManager);;
公共AuthController(UserManager UserManager,
签名管理器(签名管理器)
{
这是.\u userManager=userManager;
这个.\u signManager=signManager;
}
下面是startup.cs中的ConfigureServices:

public void ConfigureServices(IServiceCollection services)
    {
        // Add framework services.
        services.AddApplicationInsightsTelemetry(Configuration);
        services.Configure<AppConfig>(Configuration.GetSection("AppSettings"));

        //var provider = HttpContext.ApplicationServices;
        //var someService = provider.GetService(typeof(ISomeService));


        services.AddDbContext<Providers.Database.EFProvider.DataContext>(options => options
            .UseSqlServer(Configuration.GetConnectionString("DefaultConnection"),
                 b => b.MigrationsAssembly("Automobile.Server")
            ));


        services.AddIdentity<IdentityUser, IdentityRole>(options =>
        {
            options.User.RequireUniqueEmail = false;
        })
        .AddEntityFrameworkStores<Providers.Database.EFProvider.DataContext>()
        .AddDefaultTokenProviders(); 
        //services.AddScoped<SignInManager<Automobile.Models.Account>, SignInManager<Automobile.Models.Account>>();
        //services.AddScoped<UserManager<Automobile.Models.Account>, UserManager<Automobile.Models.Account>>();

        services.AddMvc();
        App.Service = services.BuildServiceProvider();

        // Adds a default in-memory implementation of IDistributedCache.
        services.AddDistributedMemoryCache();

        services.AddSession(options =>
        {
            // Set a short timeout for easy testing.
            options.IdleTimeout = TimeSpan.FromSeconds(10);
            options.CookieHttpOnly = true;
        });

    }
public void配置服务(IServiceCollection服务)
{
//添加框架服务。
services.AddApplicationInsightsTelemetry(配置);
services.Configure(Configuration.GetSection(“AppSettings”);
//var provider=HttpContext.ApplicationServices;
//var someService=provider.GetService(typeof(ISomeService));
AddDbContext(选项=>options
.UseSqlServer(Configuration.GetConnectionString(“DefaultConnection”),
b=>b.migrationassembly(“Automobile.Server”)
));
服务.附加性(选项=>
{
options.User.RequireUniqueEmail=false;
})
.AddEntityFrameworkStores()
.AddDefaultTokenProviders();
//services.addScope();
//services.addScope();
services.AddMvc();
App.Service=services.BuildServiceProvider();
//添加IDistributedCache的默认内存内实现。
AddDistributedMemoryCache();
services.AddSession(选项=>
{
//设置一个较短的超时时间以便于测试。
options.IdleTimeout=TimeSpan.FromSeconds(10);
options.CookieHttpOnly=true;
});
}

您需要在SignInManager、UserManager和services.AddIdentity中使用相同的用户数据模型。如果您使用自己的自定义应用程序角色模型类,则相同的原则是正确的

所以,改变

services.AddIdentity<IdentityUser, IdentityRole>(options =>
    {
        options.User.RequireUniqueEmail = false;
    })
    .AddEntityFrameworkStores<Providers.Database.EFProvider.DataContext>()
    .AddDefaultTokenProviders();
services.AddIdentity(选项=>
{
options.User.RequireUniqueEmail=false;
})
.AddEntityFrameworkStores()
.AddDefaultTokenProviders();

services.AddIdentity(选项=>
{
options.User.RequireUniqueEmail=false;
})
.AddEntityFrameworkStores()
.AddDefaultTokenProviders();

为了明确答案:

如果在startup.cs:
services.AddIdentity()

然后,在注入控制器时,必须在控制器中使用相同的类:

public AccountController(UserManager<ApplicationUser> userManager)
publiccountcontroller(UserManager UserManager)
如果您使用其他类别,例如:

public AccountController(UserManager<IdentityUser> userManager)
publiccountcontroller(UserManager UserManager)
然后您将得到以下错误:

InvalidOperationException:无法解析类型为“Microsoft.AspNetCore.Identity.UserManager`1[IdentityUser]”的服务


因为您在启动时使用了
ApplicationUser
,而不是
IdentityUser
,所以这种类型没有在注射系统中注册。

这与原始帖子有点无关,但因为谷歌把您带到了这里。。。如果您遇到此错误并且正在使用:

services.AddIdentityCore<YourAppUser>()

您需要将
TUser
TRole
替换为这些的实现,或者替换为默认的
IdentityUser
IdentityRole
不要忘记在ConfigureServices中添加角色管理器

services.AddDefaultIdentity<IdentityUser>()
    .AddRoles<IdentityRole>() // <--------
    .AddDefaultUI(UIFramework.Bootstrap4)
    .AddEntityFrameworkStores<ApplicationDbContext>();
services.AddDefaultIdentity()
.AddRoles()//如果您使用的是“IdentityServer”,则IdentityServer会对用户进行身份验证并授权客户端。默认情况下,IdentityServer实际上与用户管理无关。但也有一些人支持这一观点

因此,您需要添加:

services.AddIdentityServer()
    .AddAspNetIdentity<ApplicationUser>();
services.AddIdentityServer()
.addAsNetIdentity();

您可以在启动类内的ConfigureServices中分别设置IdentityUser和IdentityRole,如下所示:

services.AddDefaultIdentity<IdentityUser>()
    .AddRoles<IdentityRole>()
    .AddEntityFrameworkStores<ApplicationDbContext>();
services.AddDefaultIdentity()
.AddRoles()
.AddEntityFrameworkStores();

您可以直接配置到AddIdentity:

services.AddIdentity<IdentityUser, IdentityRole>()
        .AddEntityFrameworkStores<ApplicationDbContext>();
services.AddIdentity()
.AddEntityFrameworkStores();

您需要用以下内容更新statupp.cs类

服务.额外性() .AddEntityFrameworkStores()


这里:ApplicationUser是我的自定义模型类。

在我看来,您正在注册
IdentityUser
作为基本用户类,但随后您使用的是
Automobile.Models.Account
,当然,ASP.NET在任何地方都没有注册它Identity@FedericoDipuma非常感谢:)解决了。你是怎么解决的@Lobato in services.AddIdentity只需将IdentityUser替换为您的身份用户class@OMID你为什么不发表你的评论作为答案,这救了我,但在RnD的一些严重问题之后..我有类似的问题,我在AddIdentity方法中定义了我的客户用户和角色,我仍然得到相同的错误。知道为什么吗?我可以在一个单独的线程中发布我的代码。实际上,我解决了这个问题,但是现在在创建DB连接时遇到了一个问题。我将发布此问题。如果您的连接字符串设置不正确,请在您的帖子中查看我的答案。这对所有引用都很重要,因此如果您为asp.net core 2.1实现新的Razor标识以替换旧的标识系统,无论在哪里使用,您都需要将诸如SignInManager之类的自动实现替换为SignInManager。这可能很烦人。此外,您还需要从普通/Account/Login重新路由到/Identity/Account/Login等。以下是一些帮助提示;)只有一个代码答案,OP和其他人将很难了解这个问题,考虑编辑你的答案,并添加一个解释,这对我来说是有效的。似乎有必要
services.AddDefaultIdentity<IdentityUser>()
    .AddRoles<IdentityRole>()
    .AddEntityFrameworkStores<ApplicationDbContext>();
services.AddIdentity<IdentityUser, IdentityRole>()
        .AddEntityFrameworkStores<ApplicationDbContext>();