如何在 net.core 3.1 上使用 DI 正确配置身份核心?

How to configure properly Identity core with DI on net.core 3.1?

我在 ASP.NET core 3.1 上开始了一个宠物项目,创建了 MVC 项目和一堆库。

我还创建了:

类 继承自 AspNetCore.Identity。但是我在将 类 包含到 DI container 中时遇到了问题。每次我收到此 InvalidOpertationException 信息时:在尝试激活 JournalUserManager.

时无法解析类型 Microsoft.AspNetCore.Identity.IUserStore 的服务

这是我的代码示例:

`services.AddIdentityCore<JournalUser>(options =>
 {
    options.User.RequireUniqueEmail = true;
    options.Lockout.MaxFailedAccessAttempts = 3;
    options.Password.RequireNonAlphanumeric = false;
    options.Password.RequireUppercase = false;
 })
   .AddRoles<JournalRole>()
   .AddRoleManager<JournalRoleManager>()
   .AddUserManager<JournalUserManager>();`

有什么解决方法的建议吗?

查看 custom storage providers guide

Reconfigure app to use a new storage provider中有这个设置IUserStoreIRoleStore

的例子
public void ConfigureServices(IServiceCollection services)
{
    // Add identity types
    services.AddIdentity<ApplicationUser, ApplicationRole>()
        .AddDefaultTokenProviders();

    // Identity Services
    services.AddTransient<IUserStore<ApplicationUser>, CustomUserStore>();
    services.AddTransient<IRoleStore<ApplicationRole>, CustomRoleStore>();
    string connectionString = Configuration.GetConnectionString("DefaultConnection");
    services.AddTransient<SqlConnection>(e => new SqlConnection(connectionString));
    services.AddTransient<DapperUsersTable>();

    // additional configuration
}