.net core .NET核心标识多个用户类型

.net core .NET核心标识多个用户类型,.net-core,asp.net-identity,.net Core,Asp.net Identity,我有多个类(A、B和C),每个类都扩展了IdentityUser。我还有一个名为UserRole的类,它扩展了IdentityRole 以下是我的DbContext: public sealed class EntityDbContext: DbContext { public DbSet<A> As { get; set; } public DbSet<B> Bs { get; set; } public DbSet<C> Cs

我有多个类(
A
B
C
),每个类都扩展了
IdentityUser
。我还有一个名为
UserRole
的类,它扩展了
IdentityRole

以下是我的
DbContext

public sealed class EntityDbContext: DbContext
{
    public DbSet<A> As { get; set; }

    public DbSet<B> Bs { get; set; }

    public DbSet<C> Cs { get; set; }

}
我还有以下商店:

public class AUserStore : UserStore<A, UserRole, EntityDbContext, Guid> { } 
public class BUserStore : UserStore<B, UserRole, EntityDbContext, Guid> { } 
public class CUserStore : UserStore<C, UserRole, EntityDbContext, Guid> { } 
公共类AUserStore:UserStore{}
公共类BUserStore:UserStore{}
公共类CUserStore:UserStore{}
以下是我得到的错误:

指定的参数超出了有效值的范围。(参数 返回类型为AUserStore的实例“AUserStore”无法强制转换为 IUserStore')

我不知道我所做的是否可能。谢谢你的帮助或提示


更新

我想我成功了:

class GenericUserRoleStore : RoleStore<UserRole, EntityDbContext, Guid> { }

        services.AddIdentity<A, UserRole>()
            .AddDefaultTokenProviders()
            .AddUserStore<AUserStore>()
            .AddRoleStore<GenericUserRoleStore>();

        services.AddIdentityCore<B>()
            .AddRoles<UserRole>()
            .AddDefaultTokenProviders()
            .AddUserStore<BUserStore>()
            .AddRoleStore<GenericUserRoleStore>();

        services.AddIdentityCore<C>()
            .AddRoles<UserRole>()
            .AddDefaultTokenProviders()
            .AddUserStore<CUserStore>()
            .AddRoleStore<GenericUserRoleStore>();
class GenericUserRoleStore:RoleStore{}
服务.额外性()
.AddDefaultTokenProviders()
.AddUserStore()
.AddRoleStore();
services.AddIdentityCore()
.AddRoles()
.AddDefaultTokenProviders()
.AddUserStore()
.AddRoleStore();
services.AddIdentityCore()
.AddRoles()
.AddDefaultTokenProviders()
.AddUserStore()
.AddRoleStore();

关于
附加属性
附加属性核心
的评论都有以下内容:

为指定的用户和角色类型添加和配置标识系统

以及

  • 比较和的源代码
  • 查看项目模板中的默认代码:

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(
                Configuration.GetConnectionString("DefaultConnection")));
        services.AddIdentity<ApplicationUser, ApplicationRole>(options => options.SignIn.RequireConfirmedAccount = true)
            .AddEntityFrameworkStores<ApplicationDbContext>();
        ....
    }
    
    当您需要用户时:

    var allUsers = await _dbContext.Users.ToListAsync();
    var allProjectManagers = await _dbContext.ProjectManagers.ToListAsync();
    var allDevelopers = await _dbContext.Developers.ToListAsync();
    var allTesters = await _dbContext.Testers.ToListAsync();
    
    接下来要配置的是UserManager,而不是IUserStore

    public class ApplicationUserManager<TUser, TRole>
        where TUser : ApplicationUser
        where TRole : ApplicationRole
    
    {
        private readonly ApplicationDbContext _context;
        private readonly UserManager<TUser> _userManager;
        private readonly RoleManager<TRole> _roleManager;
    
        public ApplicationUserManager(ApplicationDbContext context,
            UserManager<TUser> userManager,
            RoleManager<TRole> roleManager)
        {
            _context = context;
            _userManager = userManager;
            _roleManager = roleManager;
        }
        //customize your own base logics here.
    
    }
    
    public class DeveloperUserManager : ApplicationUserManager<Developer, ApplicationRole>
    {
    
    }
    
    public class DocumenterUserManager : ApplicationUserManager<Documenter, ApplicationRole>
    {
    
    }
    
    公共类ApplicationUserManager
    其中TUser:ApplicationUser
    where-TRole:ApplicationRole
    {
    私有只读应用程序的bContext\u上下文;
    私有只读用户管理器_UserManager;
    专用只读角色管理器(RoleManager);
    公共应用程序服务器管理器(ApplicationDbContext上下文,
    用户管理器用户管理器,
    角色经理(角色经理)
    {
    _上下文=上下文;
    _userManager=userManager;
    _roleManager=roleManager;
    }
    //在这里定制您自己的基本逻辑。
    }
    公共类developerManager:ApplicationUserManager
    {
    }
    公共类文档管理员:ApplicationUserManager
    {
    }
    

    享受吧。

    这个问题也可以问多个用户,以防对您有所帮助。这个问题问的是铸造,这与您的错误类似
    var allUsers = await _dbContext.Users.ToListAsync();
    var allProjectManagers = await _dbContext.ProjectManagers.ToListAsync();
    var allDevelopers = await _dbContext.Developers.ToListAsync();
    var allTesters = await _dbContext.Testers.ToListAsync();
    
    public class ApplicationUserManager<TUser, TRole>
        where TUser : ApplicationUser
        where TRole : ApplicationRole
    
    {
        private readonly ApplicationDbContext _context;
        private readonly UserManager<TUser> _userManager;
        private readonly RoleManager<TRole> _roleManager;
    
        public ApplicationUserManager(ApplicationDbContext context,
            UserManager<TUser> userManager,
            RoleManager<TRole> roleManager)
        {
            _context = context;
            _userManager = userManager;
            _roleManager = roleManager;
        }
        //customize your own base logics here.
    
    }
    
    public class DeveloperUserManager : ApplicationUserManager<Developer, ApplicationRole>
    {
    
    }
    
    public class DocumenterUserManager : ApplicationUserManager<Documenter, ApplicationRole>
    {
    
    }