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

InvalidOperationException:无法解析"Microsoft"类型的服务.AspNetCore.身份用户管理器'1[Automobile.Models.Account]'正在try 激活'Automobile'.服务器控制器.授权控制器'.

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

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;
    }

下面是startup中的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;
        });

    }

推荐答案

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

所以,改变

services.AddIdentity<IdentityUser, IdentityRole>(options =>
    {
        options.User.RequireUniqueEmail = false;
    })
    .AddEntityFrameworkS到res<Providers.Database.EFProvider.DataContext>()
    .AddDefaultTokenProviders();

services.AddIdentity<Au到mobile.Models.Account, IdentityRole>(options =>
    {
        options.User.RequireUniqueEmail = false;
    })
    .AddEntityFrameworkS到res<Providers.Database.EFProvider.DataContext>()
    .AddDefaultTokenProviders();

Asp.net相关问答推荐

使用 aspnet core SignalR 客户端为 VSIX 项目.

禁用托管优化并重新启动调试实际上在 Visual Studio 中更改了哪些设置?

创建项目 ASP.NET Core (.NET Core) 和 ASP.NET Core (.NET Framework) 有什么区别

无法加载文件或程序集.无效指针(HRESULT 异常:0x80004003 (E_POINTER))

如何将 viewbag 显示为 html?

如何在 GridView 单元格中的(即
)中呈现解码的 HTML

判断 asp.net mvc 4 应用程序中的 ssl 协议、密码和其他属性

self 跟踪实体与 POCO 实体

HttpResponse 的区别:SetCookie、AppendCookie、Cookies.Add

asp.net dropdownlist - 在 db 值之前添加空行

ASP.NET Membership:如何将用户设置为已登录

OnCheckedChanged 事件未触发

如何在集线器类之外获取 SignalR 用户连接 ID?

在使用网络服务器加载它们之前,如何编译 Asp.Net Aspx 页面?

如何从 JS 访问 ViewBag

回发后动态创建的控件丢失数据

如何从 HTTP 请求中获取 MAC 地址?

与 Twitter Bootstrap Bundle 的 ASP.NET MVC4

如何获取程序集的最后修改日期?

MVC4 - 当优化设置为 true 时Bundle 不起作用