Simplify ASP.NET Core authorization based on permission
- Permission authorization.
- Enum based permission authorization.
- Claim, enum based permission authorization.
dotnet add package Wd3w.AspNetCore.Permission
If your permissions are constantly changing, this strategy is for you.
public class CustomPermissionProvider : IPermissionNameProvider
{
public async Task<IEnumerable<string>> GetPermissionsFromAuthorizationContextAsync(AuthorizationHandlerContext context)
{
// do something...
}
public async Task<IEnumerable<string>> GetAllPermissionsAsync()
{
// return all permissions
}
}
// on Startup.cs
public void ConfigureServices(IServiceCollection services)
{
// Add your custom
services.AddAuthorization();
services.AddPermissionServices<CustomPermissionProvider>(); // register service scoped as default
// services.AddPermissionServices<CustomPermissionProvider>(ServiceLifetime.Singleton);
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseAuthorization();
}
// on some controller
[Permission("ReadSomeResource")]
[HttpGet]
public Task<ActionResult> GetSomeResourceAsync()
{
}
If you need(or want) to setup type-safe permission authorization, this strategy is good option. Type-Safe permission is meaning all permission managed by source code.
public enum Permissions
{
[Permission("ReadableSomeResource")]
ReadableSomeResource
}
public class CheckPermissionAttribute : PermissionAttribute
{
public CheckPermissionAttribute(Permissions permission) : base(EnumBasedPermissionHelper.GetPermission(permission))
{
}
}
public class CustomPermissionProvider : EnumPermissionProviderBase<Permissions>
{
public override Task<IEnumerable<string>> GetPermissionsFromAuthorizationContextAsync(AuthorizationHandlerContext context)
{
// do something..
}
}
// on Startup.cs
public void ConfigureServices(IServiceCollection services)
{
// Add your custom
services.AddAuthorization();
services.AddEnumBasedPermissionServices<Permissions, CustomPermissionProvider>(); // register service scoped as default
// services.AddPermissionServices<CustomPermissionProvider>(ServiceLifetime.Singleton);
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseAuthorization();
}
[CheckPermission(Permissions.ReadableSomeResource)]
[HttpGet]
public Task<ActionResult> GetSomeResourceAsync()
{
}
As conventionally, we can use claims of ClaimsPricipal
to check permission of user.
public enum Permissions
{
[Permission("ReadableSomeResource")]
ReadableSomeResource
}
public class CheckPermissionAttribute : PermissionAttribute
{
public CheckPermissionAttribute(Permissions permission) : base(EnumBasedPermissionHelper.GetPermission(permission))
{
}
}
// on Startup.cs
public void ConfigureServices(IServiceCollection services)
{
// Add your custom
services.AddAuthorization();
services.AddClaimEnumBasedPermissionServices<Permissions>(); // register service scoped as default
// services.AddPermissionServices<CustomPermissionProvider>(ServiceLifetime.Singleton);
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseAuthorization();
}
[CheckPermission(Permissions.ReadableSomeResource)]
[HttpGet]
public Task<ActionResult> GetSomeResourceAsync()
{
}
public async Task<ClaimsPrincipal> GetClaimsPrincialAsync(string token)
{
// somewhere your auth service that create principal of authenticated user.
return new ClaimsPrincipal(new ClaimsIdentity(new [] {
new Claims(EnumBasedPermissionHelper.PermissionClaimType, "ReadableSomeResource") // here!
}));
}
This package versioning strategy is following AspNetCore package version. When there are new release about AspNetCore, This package also will be update.
Only minor version is mismatched AspNetCore, that will use for fixing bugs and some changes.
MIT License
Copyright (c) 2020 WDWWW
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.