【问题标题】:ASP.NET Core Identity does not inject UserManager<ApplicationUser>ASP.NET Core 标识不注入 UserManager<ApplicationUser>
【发布时间】:2022-01-12 22:16:17
【问题描述】:

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

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

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

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

我的期望是现在

     UserManager<ApplicationUser>

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

但是,当将以下代码添加到控制器时 私有用户管理器_userManager;

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

... 对 api 的每次调用都以异常结束: HttpRequestException:响应状态码不表示成功:500(内部服务器错误)。

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

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

附:从“异常设置”窗口启用所有异常后,我得到了这个:

抛出异常:
中的“System.InvalidOperationException” Microsoft.Extensions.DependencyInjection.dll

附加信息:无法解析服务类型 'Namespace.Data.ApplicationDbContext' 尝试激活时 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore`4 [命名空间.模型。 ApplicationUser,Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityRole,Namespace.Data.ApplicationDbContext,System.String]'。

【问题讨论】:

  • 添加完整服务器错误
  • 当我遇到这个错误时,问题是我的 ApplicationDbContext 不是从 IdentityDbContext 派生的。它只是从没有通用类型 ApplicationUser 的 IdentityDbContext 派生而来的。

标签: c# asp.net-web-api asp.net-core asp.net-identity asp.net-core-mvc


【解决方案1】:

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

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

编辑 services.AddIdentity&lt;ApplicationUser, IdentityRole&gt;() 行之前是否也有这一行?

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

 }

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

【讨论】:

  • 我在上面提到我没有(因为我不打算将它用于身份验证 - 仅用于用户存储)。然而,即使在实验性地添加它之后,结果也是一样的。
  • 您的编辑修复了它,现在我很尴尬。我会在剩下的时间里隐藏我的脸......
  • Mihail 提到添加 app.UseIdentity()。这在 CORE 2.0 中已被弃用。现在的方法是 app.UseAuthentication()。希望这会有所帮助。
  • UseAuthentication() 似乎也没有注册这些经理。
  • 如果我不使用数据库上下文怎么办?
【解决方案2】:

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

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

您还应该熟悉official documentation

【讨论】:

  • 正是这个文档声称在使用 services.AddIdentity() 后一切都可以通过 DI 获得,不幸的是它不起作用。尝试了这个解决方案,除了在 UserStore 构造函数中添加临时用户存储、密码验证器和所有可见的依赖项 - 仍然没有。
  • 好的,明白了。您能否添加问题的minimal reproducible example,以便我们可以重现您的问题并能够提供更好的答案
【解决方案3】:
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();
    ...
}

【讨论】:

    猜你喜欢
    • 2017-09-05
    • 2020-04-21
    • 1970-01-01
    • 2015-06-17
    • 2019-01-31
    • 2018-11-20
    • 1970-01-01
    • 1970-01-01
    • 2019-01-08
    相关资源
    最近更新 更多