Course contentsShow
← C#
Lesson 1213 of 1,90811. Identity, Security, and ClaimsFree lesson

Hierarchical Role Management

Implement parent-child relationships between roles for easier permission delegation.

In this lesson, you will learn how to design a role structure where child roles automatically inherit the permissions of their parent roles, simplifying complex permission management.

The Concept of Hierarchical Roles

In standard Role-Based Access Control (RBAC), roles are often flat, meaning if you have an Editor role and a Manager role, you must manually assign all Editor permissions to the Manager. As your application grows, this becomes an administrative nightmare.

Hierarchical Role Management introduces a parent-child relationship: if a user is assigned the Manager role, and Manager is a child of Editor, the system automatically resolves the user’s identity to include all claims associated with the Editor role.

Implementation Strategy

To implement this, you typically extend your Role entity to include a ParentRoleId property. During your authorization logic—specifically within your ClaimsTransformation or custom AuthorizationHandler—you recursively fetch the parent roles to aggregate the total set of permissions.

public class ApplicationRole
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int? ParentRoleId { get; set; } // The link to the parent
    public ApplicationRole Parent { get; set; }
}

When checking if a user has access, your logic should look like this:

  1. Identify the user's direct roles.
  2. Traverse the hierarchy tree upward (using the ParentRoleId) to collect all roles in the inheritance chain.
  3. Combine all unique claims found across this chain.
  4. Grant access if the required claim exists in the aggregated set.

By handling the hierarchy at the claims generation stage, your controllers remain clean. You simply check for a specific permission (e.g., [Authorize(Policy = "EditContent")]), and the framework evaluates the expanded list of inherited claims rather than just the user’s assigned role. This approach ensures that if you update the permissions on a parent role, all children automatically inherit the change without requiring a database update for every individual user account.

Key Takeaway: Hierarchical roles allow for permission inheritance, where child roles automatically assume the capabilities of their parents, significantly reducing redundancy in your authorization logic.