ASP.NET Core身份验证未注入UserManager<ApplicationUser>。

17

我有一个较旧的asp.net core身份验证数据库,希望将一个新项目(Web API)映射到该数据库。

仅为了测试,我复制了先前项目中的Models文件夹和ApplicationUser文件(ApplicationUser仅从IdentityUser继承,没有任何更改) - 做DB first似乎不是一个好主意。

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

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

我的期望是现在

     UserManager<ApplicationUser>

应该自动注入到构造函数中。

但是,当将以下代码添加到控制器时 private UserManager _userManager;

    public UserController(UserManager<ApplicationUser> userManager)
    {
        _userManager = userManager;
    }

每次调用API都以异常结束:

HttpRequestException:响应状态代码不表示成功:500(内部服务器错误)。

移除“注入”代码会导致顺利运行的Web API可以接受请求。

由于这是在我的任何代码被执行之前发生的,因此很难进行调试。您有任何想法为什么会发生这种情况吗?

附言:启用了异常设置窗口中的所有异常后,我得到了以下异常:

异常抛出:'System.InvalidOperationException' in
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]”。


添加完整的服务器错误。 - Master Programmer
当我遇到这个错误时,问题是我的 ApplicationDbContext 没有从 IdentityDbContext<ApplicationUser> 派生。它只是从没有泛型类型 ApplicationUser 的 IdentityDbContext 派生的。 - saquib adil
3个回答

11

你的 Configure 方法中是否有 app.UseIdentity(); 调用:

 public void Configure(IApplicationBuilder app, 
                       IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        /*...*/
        app.UseIdentity();
       /*...*/          
    }

编辑 您是否在services.AddIdentity<ApplicationUser, IdentityRole>() 代码行之前也有这一行?

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

 }

这应该可以正常工作。还请检查ApplicationDbContext是否继承自IdentityDbContext


我之前提到过我不打算将其用于身份验证(仅用于用户存储),然而,即使实验性地添加它,结果仍然相同。 - nikovn
2
你的修改解决了问题,现在我感到很尴尬。我会为今天剩下的时间躲起来... - nikovn
30
Mihail提到了添加app.UseIdentity(),但在CORE 2.0中已不再使用该方法。现在的方法是app.UseAuthentication()。希望这有所帮助。 - EoRaptor013
9
UseAuthentication()似乎也没有注册这些管理器。 - Sinaesthetic
如果我不使用数据库上下文会怎样? - Markus Ende
@Mihail Stancescu 这是否意味着 services.AddIdentity<ApplicationUser, IdentityRole>() 注入了 UserManager<T> - user11680003

4

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

services.AddTransient<UserManager<ApplicationUser>>();
services.AddTransient<ApplicationDbContext>();

您还应该熟悉官方文档,这与IT技术有关。请注意保留HTML标记。


2
正是这份文档声称通过使用services.AddIdentity()后,一切都可以通过DI获得,但不幸的是这并不起作用。尝试了这个解决方案,除了在UserStore构造函数中添加瞬态用户存储、密码验证器以及所有可见的依赖项之外,仍然没有任何效果。 - nikovn
1
好的,明白了。您能否添加一个 [mcve] 以便我们能够重现您的问题并提供更好的答案? - Nkosi

-1
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();
    ...
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接