iCat.Authorization 3.1.0

dotnet add package iCat.Authorization --version 3.1.0                
NuGet\Install-Package iCat.Authorization -Version 3.1.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="iCat.Authorization" Version="3.1.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add iCat.Authorization --version 3.1.0                
#r "nuget: iCat.Authorization, 3.1.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install iCat.Authorization as a Cake Addin
#addin nuget:?package=iCat.Authorization&version=3.1.0

// Install iCat.Authorization as a Cake Tool
#tool nuget:?package=iCat.Authorization&version=3.1.0                

iCat.Authorization.Web

iCat.Authorization.Web is integrated to the Policy-based authorization.<br> It customs IAuthorizationRequirement, AuthorizationHandler<> and provide provider for processing authorization-related data.

Installation

dotnet add package iCat.Authorization.Web

Configuration

Define privileges and permissions enums mapping

The defination of privileges and permissions need to follow these rules.

  1. Use bitwise values to define permissions and apply the Flags attribute on the enum.
  2. Use the Permission attribute to assign specific privileges based on the defined permissions.
    using iCat.Authorization;
    public enum PrivilegeEnum
    {
        [Permission(typeof(UserProfilePermission))]
        UserProfile = 1,
        [Permission(typeof(OrderPermission))]
        Order = 2,
        [Permission(typeof(DepartmentPermission))]
        Department = 3
    }

    [Flags]
    public enum UserProfilePermission
    {
        Add = 1,
        Edit = 2,
        ReadPartialDetail = 4,
        Delete = 8,
        ReadAllDetail = 16,
    }

    [Flags]
    public enum OrderPermission
    {
        Add = 1,
        Read = 2,
        Edit = 4,
        Delete = 8
    }

    [Flags]
    public enum DepartmentPermission
    {
        Add = 1,
        Edit = 2,
        Read = 4,
        Delete = 8
    }

Configure Requirment and Handler

Register providers and privileges/permissions using the .AddWebPermissionAuthorizationProvider<T> method, add a requirment to the policies via .AddWebPermissionsAuthorizationRequirment().<br> iCat.Authorization.Web needs to use IHttpContextAccessor to obtain the current requested privileges/permissions.

    using iCat.Authorization.Web.Extensions;
    public static void Main(string[] args)
    {
        var builder = WebApplication.CreateBuilder(args);
        var services = builder.Services;
        // Add services to the container.
        builder.Services
            .AddSingleton<IHttpContextAccessor, HttpContextAccessor>()
            .AddWebPermissionAuthorizationProvider<PrivilegeEnum>()
            .AddAuthorization(options =>
            {
                options.DefaultPolicy = new AuthorizationPolicyBuilder()
                    .AddAuthenticationSchemes(CookieAuthenticationDefaults.AuthenticationScheme)
                    .AddWebPermissionsAuthorizationRequirment()
                    .RequireAuthenticatedUser()
                    .Build();

            });

        ...

        app.Run();
    }

WebPermissionAuthorization on action

Set the permission for the action through the PermissionsAuthorization attribute.

    using iCat.Authorization.Web;
    ...

    [PermissionsAuthorization(
        DepartmentPermission.Read | DepartmentPermission.Delete,
        UserProfilePermission.Add | UserProfilePermission.Edit | UserProfilePermission.Read)]
    [HttpGet("[action]")]
    public async Task<IActionResult> GetData()
    {
        ...
    }

Obtain current user privileges, claims

The IPrivilegeProvider<T> provides methods to obtain the logged user's claim from the Privilege. <br>

    using iCat.Authorization.Web;
   [ApiController]
   [Route("[controller]")]
   public class TestController : ControllerBase
   {
        private readonly IPrivilegeProvider<PrivilegeEnum> _privilegeProvider;

       public TestController(IPrivilegeProvider<PrivilegeEnum> privilegeProvider)
       {
            _privilegeProvider = privilegeProvider ?? throw new ArgumentNullException(nameof(privilegeProvider));
       }
       
       [PermissionsAuthorization(
            DepartmentPermission.Read | DepartmentPermission.Delete,
            UserProfilePermission.Add | UserProfilePermission.Edit | UserProfilePermission.ReadPartialDetail)]
       [HttpGet("[action]")]
       public IActionResult GetData()
       {
            var claims = new List<Claim>
            {
                new Claim(ClaimTypes.Name, "TestUser"),
                new Claim("UserId", "TestId"),
                _privilegeProvider.GenerateClaim(UserProfilePermission.Add | UserProfilePermission.ReadAllDetail),
                _privilegeProvider.GenerateClaim(DepartmentPermission.Delete),
            };

            var userPrivileges = _privilegeProvider.GetCurrentUserPrivileges();
            return Ok(userPrivileges);
       }
   }
Product 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 is compatible.  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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on iCat.Authorization:

Package Downloads
iCat.Authorization.Web

Privilege Permission Authorization For Web

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
3.1.0 47 1/8/2025
2.0.0 141 6/20/2024
1.3.5 119 6/13/2024
1.3.4 156 1/24/2024
1.2.3 133 1/16/2024
1.2.2 121 1/16/2024
1.1.2 118 1/15/2024
1.0.2 152 1/8/2024 1.0.2 is deprecated because it is no longer maintained.
1.0.0 147 1/7/2024 1.0.0 is deprecated because it is no longer maintained.

Version 3.1.0