MadEyeMatt.AspNetCore.Authorization.Permissions.Abstractions
6.1.0
See the version list below for details.
dotnet add package MadEyeMatt.AspNetCore.Authorization.Permissions.Abstractions --version 6.1.0
NuGet\Install-Package MadEyeMatt.AspNetCore.Authorization.Permissions.Abstractions -Version 6.1.0
<PackageReference Include="MadEyeMatt.AspNetCore.Authorization.Permissions.Abstractions" Version="6.1.0" />
paket add MadEyeMatt.AspNetCore.Authorization.Permissions.Abstractions --version 6.1.0
#r "nuget: MadEyeMatt.AspNetCore.Authorization.Permissions.Abstractions, 6.1.0"
// Install MadEyeMatt.AspNetCore.Authorization.Permissions.Abstractions as a Cake Addin #addin nuget:?package=MadEyeMatt.AspNetCore.Authorization.Permissions.Abstractions&version=6.1.0 // Install MadEyeMatt.AspNetCore.Authorization.Permissions.Abstractions as a Cake Tool #tool nuget:?package=MadEyeMatt.AspNetCore.Authorization.Permissions.Abstractions&version=6.1.0
AspNetCore.Authorization.Permissions
A libary that add permission-based authorization.
The ASP.NET Core role-based authorization in combination with custom authorization policies is a good starting point for restricting users' access in an application, but it is very static and changes to the meaning of a role or a policy forces you to perform changes in your code and to re-deploy your application. This library aims to overcome this limitation.
To be able to dynamically change the access of a user we extend the role in a way that each role is made up of several fine gained permissions. Throughout the documentation and the sample applications we will use the following roles and permissions.
Role | Invoice.Read | Invoice.Write | Invoice.Delete | Invoice.Send | Invoice.Payment |
---|---|---|---|---|---|
Boss | YES | NO | NO | NO | NO |
Manager | YES | NO | YES | NO | NO |
Employee | YES | YES | NO | YES | YES |
In this fictional company the boss can only read invoices, the manager can read and delete invoices and the employees can read, write, send invocies and can trigger the selllement of the invoice.
If we build the authentication around the three role we will hard-code the access permissions in our
codebase f.e. using the [Authorize]
attribute. But if the boss decides he also wants
to be able to delete invoices, we need to change it in the source code.
Using permissions of the role defined in the table above in the [Authorize]
attribute instead
of the roles, we can just change the role configuration in a data store and assign the permision
Invoices.Delete to the role Boss. The boss used is then able to delete invocies without
the need to change the code and re-deploy the application.
The library consists of two parts:
- The base definitions, policies and services to be able to check an authenticated users claims
(i.e.
ClaimsPrincipal
) for asigned permissions. - An implementation of the permissions API to work with ASP.NET Core Identity.
It is possible to add different storages and claims providers. A library that wanst to provide
the permissions claims just needs to implement the IClaimsProvider
interface and the storage
mechanism of course.
In addition to the basic permissions of users, this library provides an optional multi-tenant feature. This feature allows assign tenants to users. The tenant infosmations are then added to the user's claims. Storage systems can then leverage the tenant information to alter queries (Single Database with Tenant Column) or to select connection strings (Tenant per Database).
A tenant may have several roles and the permissions of those roles are added to the user's permission claims. In that way additional permissions can be added to individual claims. The tenant sample applications provide tenants with a distict tenant role assigned which provide the following additional tenant permissions. Each tenant represents a separate company. The roles in this example represent different plans of a SaaS application.
Role | Invoice.Statistics | Invoice.TaxExport |
---|---|---|
Free | NO | NO |
Basic | YES | NO |
Professional | YES | YES |
Using tenants, roles and permissions is a good way to define differnent sets of features, f.e. when creating different plans of a SaaS application.
Permission Usage
To configure the permissions with ASP.NET Identity and the default identity models add the following code to your application startup code. The example uses EF Core and SQLite to store the Identity models.
The users, roles and permissions are added to the storage using the ApplicationDbContext
and EF
Core migrations. The code is omitted in this document, but you can look it up in the samples code.
// ... Previous service configuration omitted.
builder.Services.AddAuthorization();
builder.Services.AddPermissionsAuthorization(options =>
{
options.AddIdentityClaimsProvider();
});
builder.Services
.AddDbContext<ApplicationDbContext>(options =>
{
options.UseSqlite("Filename=permissions.db");
})
.AddPermissionsIdentity()
.AddDefaultUI()
.AddDefaultTokenProviders()
.AddPermissionsEntityFrameworkStores<ApplicationDbContext>();
// Additional service configuration omitted ...
Restrict access based on permissions
There are several ways to rescript access in your application.
- Use the
[Authorize]
attribute to restrict access to controller actions. - Use the
HasPermission()
extension method with aClaimsPrincipal
instance. - Use the
HasPermission()
method of aIUserPermissionsService
instance.
To retrict the access to an action methods just add the [Authorize]
attribute with the permission
name as contraint.
// ASP.NET MVC controller action with attribute.
[HttpGet]
[HasPermission("Invoice.Payment")]
public IActionResult Get()
{
return this.Ok();
}
// Razor Pages with attribute.
[HasPermission("Invoices.Read")]
public class InvoicesReadModel : PageModel
{
public void OnGet()
{
}
}
// Razor Pages with extension method.
public class InvoicesReadModel : PageModel
{
public IActionResult OnGet()
{
if(!this.User.HasPermission("Invoice.Read"))
{
if(this.User.IsAuthenticated())
{
return this.Forbid();
}
return this.Challenge();
}
return this.Page();
}
}
Tenant Usage
TODO: ITenantContextAccessor
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. 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 was computed. 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. |
.NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.1 is compatible. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
- JetBrains.Annotations (>= 2022.1.0)
NuGet packages (2)
Showing the top 2 NuGet packages that depend on MadEyeMatt.AspNetCore.Authorization.Permissions.Abstractions:
Package | Downloads |
---|---|
MadEyeMatt.AspNetCore.Authorization.Permissions
A libary that adds permission-based authorization. |
|
MadEyeMatt.Extensions.Identity.Permissions.Core
A libary that adds permission-based authorization. |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
9.0.1 | 121 | 11/16/2024 |
9.0.0 | 109 | 11/14/2024 |
8.7.6 | 204 | 11/1/2024 |
8.7.5 | 438 | 7/9/2024 |
8.7.4 | 364 | 6/2/2024 |
8.7.3 | 260 | 5/24/2024 |
8.7.2 | 541 | 4/18/2024 |
8.7.1 | 441 | 3/19/2024 |
8.6.0 | 856 | 1/26/2024 |
8.5.2 | 1,472 | 1/4/2024 |
8.5.1 | 1,008 | 11/24/2023 |
8.5.0 | 766 | 11/16/2023 |
8.4.4 | 955 | 7/20/2023 |
8.4.3 | 1,394 | 4/25/2023 |
8.4.2 | 1,215 | 4/13/2023 |
8.4.1 | 890 | 4/13/2023 |
8.4.0 | 885 | 4/12/2023 |
8.3.4 | 875 | 4/9/2023 |
8.3.3 | 957 | 4/2/2023 |
8.3.2 | 868 | 4/2/2023 |
8.3.1 | 819 | 4/2/2023 |
8.3.0 | 948 | 3/31/2023 |
8.2.3 | 910 | 3/31/2023 |
8.2.2 | 883 | 3/31/2023 |
8.2.1 | 875 | 3/31/2023 |
8.2.0 | 891 | 3/30/2023 |
8.1.1 | 1,007 | 3/22/2023 |
8.1.0 | 840 | 3/21/2023 |
8.0.0 | 799 | 3/16/2023 |
7.1.1 | 1,045 | 2/24/2023 |
7.1.0 | 1,216 | 1/18/2023 |
7.0.0 | 1,759 | 11/20/2022 |
6.1.0 | 1,531 | 10/12/2022 |
6.0.4 | 3,568 | 6/7/2022 |
6.0.3 | 1,296 | 6/3/2022 |
6.0.2 | 3,400 | 5/4/2022 |
6.0.1 | 1,353 | 5/3/2022 |
6.0.0 | 1,291 | 5/3/2022 |