C# ASP.NET核心标识不注入UserManager<;应用程序用户>;

C# ASP.NET核心标识不注入UserManager<;应用程序用户>;,c#,asp.net-web-api,asp.net-core,asp.net-identity,asp.net-core-mvc,C#,Asp.net Web Api,Asp.net Core,Asp.net Identity,Asp.net Core Mvc,我有一个旧的asp.net核心标识数据库,我想将一个新项目(web api)映射到它 为了进行测试,我复制了Models文件夹和来自上一个项目的ApplicationUser文件(ApplicationUser只是从IdentityUser继承,没有任何更改)——首先使用DB似乎是个坏主意 我正在ConfigureServices中注册标识(但我没有将其添加到管道中,因为我的唯一目的是使用UserStore) services.AddIdentity() .AddEntityFrameworkS

我有一个旧的asp.net核心标识数据库,我想将一个新项目(web api)映射到它

为了进行测试,我复制了Models文件夹和来自上一个项目的ApplicationUser文件(ApplicationUser只是从IdentityUser继承,没有任何更改)——首先使用DB似乎是个坏主意

我正在ConfigureServices中注册标识(但我没有将其添加到管道中,因为我的唯一目的是使用UserStore)

services.AddIdentity()
.AddEntityFrameworkStores()
.AddDefaultTokenProviders();
我的期望是现在

     UserManager<ApplicationUser>
UserManager
…应自动注入构造函数中

但是,在向控制器添加以下代码时 私人用户管理器(UserManager);

    public UserController(UserManager<ApplicationUser> userManager)
    {
        _userManager = userManager;
    }
公共用户控制器(用户管理器用户管理器) { _userManager=userManager; } 。。。对api的每次调用都以一个异常结束: HttpRequestException:响应状态代码不表示成功:500(内部服务器错误)

删除“注入”代码将导致能够接受请求的web api平稳运行

很难调试,因为这发生在我的任何代码到达之前。知道为什么会这样吗

另外,在从“异常设置”窗口启用所有异常后,我得到了以下结果:

引发异常:
Microsoft.Extensions.DependencyInjection.dll

其他信息:无法解析类型的服务 试图激活时出现“Namespace.Data.ApplicationDbContext” 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore`4[Namespace.Models。 ApplicationUser,Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityRole,Namespace.Data.ApplicationDbContext,System.String]'


你有
app.UseIdentity()吗调用
Configure
方法:

 public void Configure(IApplicationBuilder app, 
                       IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        /*...*/
        app.UseIdentity();
       /*...*/          
    }
编辑
services.AddIdentity()
行之前是否也有这一行

 public void ConfigureServices(IServiceCollection services)
 {
        // Add framework services.
        services.AddDbContext<ApplicationDbContext>(options =>
             options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

 }
public void配置服务(IServiceCollection服务)
{
//添加框架服务。
services.AddDbContext(选项=>
options.UseSqlServer(Configuration.GetConnectionString(“DefaultConnection”));
}

这应该可以。另外,请检查
ApplicationDbContext
是否继承自
IdentityDbContext

DI容器无法解析依赖项。将其添加到服务集合

services.AddTransient<UserManager<ApplicationUser>>();
services.AddTransient<ApplicationDbContext>();
services.AddTransient
public void配置服务(IServiceCollection服务){
...
var identityBuilder=services.AddIdentityCore(用户=>
{
//配置标识选项
user.Password.RequireDigit=true;
user.Password.RequireLowercase=false;
user.Password.RequireUppercase=false;
user.Password.RequireNonAlphanumeric=false;
user.Password.RequiredLength=6;
});
identityBuilder=newidentitybuilder(identityBuilder.UserType,typeof(IdentityRole),identityBuilder.Services);
identityBuilder.AddEntityFrameworkStores().AddDefaultTokenProviders();
...
}

add full server error出现此错误时,问题是我的ApplicationDbContext不是从IdentityDbContext派生的。它只是从IdentityDbContext派生而来,没有泛型类型ApplicationUser。正是这篇文档声称,在使用services.AddIdentity()之后,所有东西都可以通过DI获得,但不幸的是,它不起作用。除了在UserStore构造函数中添加临时用户存储、密码验证器和所有可见的依赖项之外,还尝试了此解决方案-仍然没有。明白了。您能否添加问题的一部分,以便我们能够重现您的问题,并能够提供更好的答案?我在上文中提到过,但我没有这样做(因为我不打算将其用于仅限用户存储的身份验证)。然而,即使在实验中添加了它,结果还是一样的。你的编辑修复了它,现在我很尴尬。今天剩下的时间我都会把脸藏起来……Mihail提到添加app.UseIdentity()。这在Core2.0中已被弃用。现在的方法是app.UseAuthentication()。希望这有帮助。
UseAuthentication()
似乎也没有注册这些管理器。如果我不使用db上下文怎么办?
services.AddTransient<UserManager<ApplicationUser>>();
services.AddTransient<ApplicationDbContext>();
public void ConfigureServices(IServiceCollection services){
...
var identityBuilder = services.AddIdentityCore<ApplicationUser>(user =>
            {
                // configure identity options
                user.Password.RequireDigit = true;
                user.Password.RequireLowercase = false;
                user.Password.RequireUppercase = false;
                user.Password.RequireNonAlphanumeric = false;
                user.Password.RequiredLength = 6;
            });
            identityBuilder = new IdentityBuilder(identityBuilder.UserType, typeof(IdentityRole), identityBuilder.Services);
            identityBuilder.AddEntityFrameworkStores<DbContext>().AddDefaultTokenProviders();
    ...
}