【问题标题】:asp.net Identity Roles organisation unit structure creationasp.net Identity Roles 组织单元结构创建
【发布时间】:2020-09-07 10:59:10
【问题描述】:

是否可以像在 Active Directory 中那样将通过 ASP.NET 身份角色创建的用户放入组织单元?我希望能够允许用户进入部门,该部门的经理只能在他们自己的部门内访问其他用户的数据。例如,人力资源经理应该能够访问所有人力资源员工的数据,但不能访问运营或 IT 员工。然后,我想允许经理更高级别地访问每个人的数据。 ASP.NET 的角色和身份是否允许这样做?

我应该使用声明将用户归入部门并使用策略将人员添加到管理中吗?

它应该作为一个树结构,类似于您可以在 Active Directory 中获得的结构。但是,我在 asp.net 身份中找不到允许组织选项的选项。

【问题讨论】:

    标签: c# asp.net asp.net-core asp.net-identity asp.net-roles


    【解决方案1】:

    是的,这是可能的。 Asp.NET 标识允许您使用从IdentityUserIdentityRole 继承的类

    例如,您可以通过创建一个继承自 IdentityUser 的类来添加对用户层次结构的支持

    public class AppUser : IdentityUser
    {
        public string ManagerId { get; set; }
    
        public AppUser Manager { get; set; }
    
        public ICollection<AppUser> DirectSubordinates;
    }
    

    然后将您的ApplicationDbContext 更改为从IdentityDbContext&lt;AppUser&gt; 继承,而不是从默认的IdentityDbContext 继承

    public class ApplicationDbContext : IdentityDbContext<AppUser>
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
            : base(options)
        {
        }
    }
    

    更改层次结构后,使用dotnet-ef 工具添加迁移,然后更新您的数据库。

    您还可以使用从IdentityRole 继承的类,如下所示:

    public class ApplicationDbContext : IdentityDbContext<AppUser, AppRole, string>
    

    其中AppRole 是一个继承自IdentityRole 的类

    【讨论】:

      猜你喜欢
      • 2017-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-24
      相关资源
      最近更新 更多