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

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相关问答推荐

在 Web.Config 的 Location Path 元素中指定多个目录

如何将 viewbag 显示为 html?

捕获对 ASP.NET ASMX Web 服务的 SOAP 请求

OnCheckedChanged 事件未触发

RestSharp 获取请求的完整 URL

使用 jQuery.ajax() 时如何处理错误?

异步编程与线程有什么不同?

使用 JSON.NET 解析 json 字符串

使用域用户的 SQL 连接字符串?

您对 Windows Workflow Foundation 有何体验?

如何从 Asp.net Mvc-3 发送邮箱?

解析器错误:无法创建类型

<%# Eval("State") %> 或 <%# DataBinder.Eval(Container.DataItem, "state")%>

如何使用 encodeURIComponent() 解码在 JS 中编码的 HTML?

使用 Twitter Bootstrap 在 @html.actionlink 中显示 html

在 ASP.NET MVC 中模拟 User.Identity

错误处理(向客户端发送 ex.Message)

ASP.NET MVC2/3 中runAllManagedModulesForAllRequests的正确用法是什么?

在 ASP.NET 中使用 SecureString 有什么好处吗?

您将如何将 ASP.Net MVC 嵌入到现有的网站项目中?