RbacAuthorization 1.0.1
See the version list below for details.
dotnet add package RbacAuthorization --version 1.0.1
NuGet\Install-Package RbacAuthorization -Version 1.0.1
<PackageReference Include="RbacAuthorization" Version="1.0.1" />
paket add RbacAuthorization --version 1.0.1
#r "nuget: RbacAuthorization, 1.0.1"
// Install RbacAuthorization as a Cake Addin #addin nuget:?package=RbacAuthorization&version=1.0.1 // Install RbacAuthorization as a Cake Tool #tool nuget:?package=RbacAuthorization&version=1.0.1
RbacAuthorization
A simple role based access control library for single and multi tenant applications.
Single Tenant Application
The below task management application has two types of users, Supervisors and Assistants. Only supervisors can create and delete tasks while both can read and update the tasks.
Endpoint | Permission | Roles |
---|---|---|
POST /tasks | Tasks.Create | MyApp.Supervisor |
GET /tasks | Tasks.Read | MyApp.Assistant <br> MyApp.Supervisor |
PUT /tasks/{taskId} | Tasks.Update | MyApp.Assistant <br> MyApp.Supervisor |
DELETE /tasks/{taskId} | Tasks.Delete | MyApp.Supervisor |
Configuration steps
- Add the RbacAuthorization Nuget package.
dotnet add package RbacAuthorization
- Define your permissions. These can use any format you like but typically include a resource and an action. For example:
public static class Permissions
{
public const string TasksCreate = "Tasks.Create";
public const string TasksRead = "Tasks.Read";
public const string TasksUpdate = "Tasks.Update";
public const string TasksDelete = "Tasks.Delete";
}
- Define your roles. These can also use any format you like but typically include the app name and a job role. For example:
public static class Roles
{
public const string Supervisor = "MyApp.Supervisor";
public const string Assistant = "MyApp.Assistant";
}
- Define a policy to map your roles to permissions. In the below example the application has two types of users, Supervisors and Assistants. Only supervisors can create and delete tasks while both can read and update the tasks.
builder.Services.AddRbacAuthorization(builder.Configuration, options =>
{
options.Policy = new StaticPolicyBuilder()
.AddRolePermissions(Roles.Supervisor, Permissions.TasksCreate, Permissions.TasksRead, Permissions.TasksUpdate, Permissions.TasksDelete)
.AddRolePermissions(Roles.Assistant, Permissions.TasksRead, Permissions.TasksUpdate)
.Build();
});
- Assign the permissions to your controller actions using the standard authorize attribute:
app.MapGet("/tasks", [Authorize(Permissions.TasksRead)] () =>
{
return Results.Ok(tasks.GetAll());
});
Configure your Identity Provider to include the relevant roles as
role
claims for your users. This typically involves creating a group with the name of each role and assigning them to your users.
Multi Tenant Application
This multi tenant example builds on top the single tenant example above. There are still two types of users, Supervisors and Assistants but this time they are per tenant to provide tenant isolation.
To avoid mapping the same roles for each tenant, multi tenant roles contain a placeholder for the tenant identifier. By default the placeholder is $TenantId
but it can be changed to match what you call your tenant identifier. For example $AccountName
or $CompanyId
.
By default the library will obtain the tenant identifier from the request RouteData value named TenantId
. You can also obtain the tenant identifier from a request header or subdomain if its not included in the path.
Request | Permission | Roles |
---|---|---|
POST /{TenantId}/tasks | Tasks.Create | MyApp.$TenantId.Supervisor |
GET /{TenantId}/tasks | Tasks.Read | MyApp.$TenantId.Assistant <br> MyApp.$TenantId.Supervisor |
PUT /{TenantId}/tasks/{taskId} | Tasks.Update | MyApp.$TenantId.Assistant <br> MyApp.$TenantId.Supervisor |
DELETE /{TenantId}/tasks/{taskId} | Tasks.Delete | MyApp.$TenantId.Supervisor |
Configuration steps
- Add the RbacAuthorization Nuget package.
dotnet add package RbacAuthorization
- Define your permissions. These can use any format you like but typically include a resource and an action. For example:
public static class Permissions
{
public const string TasksCreate = "Tasks.Create";
public const string TasksRead = "Tasks.Read";
public const string TasksUpdate = "Tasks.Update";
public const string TasksDelete = "Tasks.Delete";
}
- Define your roles. These can also use any format you like but typically include the app name, tenant identifier and a job role. You can also include roles that span all tenants like a customer support role. For example:
public static class Roles
{
public const string TenantSupervisor = "MyApp.$TenantId.Supervisor";
public const string TenantAssistant = "MyApp.$TenantId.Assistant";
public const string CustomerSupport = "MyApp.CustomerSupport";
}
- Define a policy to map your roles to permissions. In the below example the application has three types of users, per tenant Supervisors and Assistants users and application wide Customer Support staff. Only supervisors can create and delete tasks in their tenant while both Supervisor and Assistants can read and update tasks in their tenant. Customer Support staff can read tasks in any tenant due to their role not being scoped to a tenant with the $TenantId placeholder.
builder.Services.AddRbacAuthorization(builder.Configuration, options =>
{
options.Policy = new StaticPolicyBuilder()
.AddRolePermissions(Roles.TenantSupervisor, Permissions.TasksCreate, Permissions.TasksRead, Permissions.TasksUpdate, Permissions.TasksDelete)
.AddRolePermissions(Roles.TenantAssistant, Permissions.TasksRead, Permissions.TasksUpdate)
.AddRolePermissions(Roles.CustomerSupport, Permissions.TasksRead)
.Build();
});
- Assign the permissions to your controller actions using the standard authorize attribute:
app.MapGet("/tasks", [Authorize(Permissions.TasksRead)] () =>
{
return Results.Ok(tasks.GetAll());
});
Configure your Identity Provider to include the relevant roles as
role
claims for your users. This typically involves creating a group with the name of each role and assigning them to your users.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net6.0
- Microsoft.Identity.Web (>= 1.16.0)
-
net7.0
- Microsoft.Identity.Web (>= 1.16.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
2.0.0 | 219 | 4/26/2024 |
2.0.0-prerelease.1 | 68 | 4/23/2024 |
1.0.1 | 228 | 4/30/2023 |
1.0.0 | 195 | 4/30/2023 |
1.0.0-alpha.4 | 137 | 12/28/2022 |
1.0.0-alpha.3 | 116 | 12/25/2022 |
1.0.0-alpha.2 | 118 | 12/22/2022 |
1.0.0-alpha.1 | 122 | 12/22/2022 |