Asp.net 基于核心角色的访问身份/角色使用户 table 自引用

Asp.net core role based access identity / roles make user table self referencing

您好,我正在尝试对访问控制系统建模,在该系统中,访问权限是按角色授予对某些操作的。我正在尝试使用具有身份和角色的 asp.net 核心。似乎默认身份带有自己的 tables 供用户使用。我拥有的用户 table 需要自我引用,因为用户将拥有同时也是用户的管理员。

是否可以使身份随附的默认 table 做到这一点?

如果你想为已经存在的 IdentityUser 添加额外的属性,你所要做的就是继承 IdentityUser class 然后添加你自己的属性然后你去启动文件和您的 dbcontext 并执行以下操作

public class InheritedUser : IdentityUser
{
   public bool HasTwoHeads { get; set;}

}

你的启动

services.AddDefaultIdentity<InheritedUser>()
                .AddEntityFrameworkStores<CustomDbContext>()                               .AddDefaultTokenProviders()
.AddDefaultUI(Microsoft.AspNetCore.Identity.UI.UIFramework.Bootstrap4);

那么你继承的DbContext

public class InheritedDbContext : IdentityDbContex<InheritedUser>
{

}