C#
Auth Playground — RBAC, ABAC & PBAC in .NET 10
A production-style learning repository that teaches Role-Based (RBAC), Attribute-Based (ABAC), and Policy-Based (PBAC) access control in ASP.NET Core, with a real OIDC/OAuth2 identity service, protected APIs, Swagger testing, Docker Compose, and comprehensive documentation.
What You'll Learn
- RBAC: Role-based endpoint protection (
[Authorize(Roles = "Admin")]) - ABAC: Attribute-based resource access (department, region, tenant, clearance level)
- PBAC: Policy-based composite rules that wrap RBAC + ABAC
- OAuth2/OIDC flows: Authorization Code + PKCE, Client Credentials
- Resource-based authorization: Imperative checks with
IAuthorizationService - Custom authorization handlers: The core of fine-grained access control in .NET
- Multi-tenant isolation: Tenant-based ABAC filtering
- Service-to-service auth: Client Credentials with scope-based policies
Quick Start
bash
docker compose up --build| Service | URL |
|---|---|
| Swagger UI | https://localhost:5001/swagger |
| API | https://localhost:5001 |
| Identity (OIDC) | https://localhost:5000 |
| OIDC Discovery | https://localhost:5000/.well-known/openid-configuration |
Test Accounts
| User | Password | Role | Department | |
|---|---|---|---|---|
| Admin | admin@authplayground.dev | Admin123! | Admin | IT |
| Manager | manager@authplayground.dev | Manager123! | Manager | Engineering |
| Employee | employee@authplayground.dev | Employee123! | Employee | Engineering |
| Auditor | auditor@authplayground.dev | Auditor123! | Auditor | Compliance |
| Regional Mgr | regional@authplayground.dev | Regional123! | RegionalManager | Engineering |
| Tenant 2 | tenant2user@authplayground.dev | Tenant2User123! | Employee | Engineering |
Architecture
text
┌─────────────┐ ┌──────────────────┐ ┌────────────┐
│ Swagger UI │────▶│ Identity Service │ │ PostgreSQL │
│ (PKCE flow) │ │ (OpenIddict) │────▶│ │
└─────┬───────┘ └──────────────────┘ └────────────┘
│ Bearer Token ▲
▼ │ Token Validation
┌─────────────┐ │
│ API Service │──────────────┘
│ (RBAC/ABAC/ │
│ PBAC) │────▶ PostgreSQL
└─────────────┘Project Structure
| Project | Purpose |
|---|---|
AuthPlayground.Domain | Core entities, enums, interfaces |
AuthPlayground.Contracts | Shared DTOs, claim/role/policy constants |
AuthPlayground.Application | Authorization handlers, business services |
AuthPlayground.Infrastructure | EF Core, PostgreSQL repositories |
AuthPlayground.Identity | OIDC/OAuth2 provider (OpenIddict) |
AuthPlayground.Api | Protected endpoints, Swagger, auth config |
Authorization Models Demonstrated
RBAC (Role-Based)
csharp
// Only Admin can manage policies
.RequireAuthorization(AppPolicies.RequireAdmin)ABAC (Attribute-Based)
csharp
// Handler checks: user.department == document.department
public class SameDepartmentHandler : AuthorizationHandler<SameDepartmentRequirement, Document>PBAC (Policy-Based, composing RBAC + ABAC)
csharp
// "CanApproveDocument" = Manager role + same department + pending status
.AddPolicy("CanApproveDocument", p => p.AddRequirements(new DocumentApprovalRequirement()))Documentation
Detailed docs with Mermaid diagrams in /docs:
| Document | Topic |
|---|---|
| Authentication vs Authorization | Core concepts |
| RBAC | Role-Based Access Control |
| ABAC | Attribute-Based Access Control |
| PBAC | Policy-Based Access Control |
| RBAC vs ABAC vs PBAC | Comparison & decision guide |
| OIDC & OAuth2 | Protocol overview |
| Authorization Code Flow | User authentication flow |
| PKCE Flow | Public client security |
| Client Credentials | Machine-to-machine flow |
| Claims, Roles, Permissions, Policies | Concept relationships |
| .NET Authorization | Implementation guide |
| Custom Handlers | Handler patterns |
| Resource-Based Auth | Imperative authorization |
| Swagger Security | Testing with Swagger |
| Architecture | System design |
| Running the Project | Setup & exercises |
| Testing Strategy | Test patterns |
| Staff-Level Cheatsheet | Quick reference |
Tech Stack
- .NET 10, ASP.NET Core Minimal APIs
- OpenIddict (OIDC/OAuth2)
- ASP.NET Core Identity
- Entity Framework Core + PostgreSQL
- Swagger/OpenAPI (Swashbuckle)
- xUnit + Moq (testing)
- Docker Compose
License
MIT — use freely for learning and reference.