Advertisement
Advanced ASP.NET Core, Authorization

How would you implement hierarchical or nested permission checks, like an organization admin having access to all of their organization's resources?

Short answer

A common approach models permissions hierarchically in your data (an OrganizationId claim plus a resource's own OrganizationId field) and implements a resource-based authorization handler that checks whether the user's organization matches (or, for a super-admin role, bypasses…

Save ASP.NET Core mock interview
Advertisement
AModel answer
A common approach models permissions hierarchically in your data (an OrganizationId claim plus a resource's own OrganizationId field) and implements a resource-based authorization handler that checks whether the user's organization matches (or, for a super-admin role, bypasses the check entirely) -- combining role-based (super-admin bypass) and resource-based (organization membership match) checks within a single handler for a complete hierarchical permission model.
protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, ResourceAccessRequirement requirement, Project resource) {
    if (context.User.IsInRole("SuperAdmin")) { context.Succeed(requirement); return Task.CompletedTask; }
    var userOrgId = context.User.FindFirst("org_id")?.Value;
    if (userOrgId == resource.OrganizationId.ToString() && context.User.IsInRole("OrgAdmin"))
        context.Succeed(requirement);
    return Task.CompletedTask;
}
Real-world example
A multi-tenant project management SaaS lets organization admins manage every project within their own organization while a platform-level SuperAdmin role bypasses organizational boundaries entirely for support purposes, both expressed in one authorization handler.
Likely follow-ups
  • How do you efficiently check organization membership without a database call on every single request?
  • How would you extend this model to support team-level (not just org-level) permissions?
Related topics