iCat.Authorization 1.3.4

dotnet add package iCat.Authorization --version 1.3.4
NuGet\Install-Package iCat.Authorization -Version 1.3.4
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="1.3.4" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add iCat.Authorization --version 1.3.4
#r "nuget: iCat.Authorization, 1.3.4"
#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=1.3.4

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

iCat.Authorization

iCat.Authorization 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

Configuration

Define permits and permissions enums mapping

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

  1. Permission needs to use bit wises value and set Flags attribute on the class.
  2. Use the Permission attribute to specify the permit's permission.
    using iCat.Authorization;
    public enum PermitEnum
    {
        [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 permits/permissions using the .AddAuthorizationPermission(typeof(Permit)) method, add a requirment to the policies via .AddAuthorizationPermissionRequirment().<br> iCat.Authorization needs to use IHttpContextAccessor to obtain the current requested permits/permissions.

    using iCat.Authorization.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>()
            .AddAuthorizationPermission(typeof(PermitEnum))
            .AddAuthorization(options =>
            {
                options.DefaultPolicy = new AuthorizationPolicyBuilder()
                    .AddAuthenticationSchemes(CookieAuthenticationDefaults.AuthenticationScheme, "Bearer")
                    .AddAuthorizationPermissionRequirment()
                    .RequireAuthenticatedUser()
                    .Build();

            });

        ...

        app.Run();
    }

AuthorizationPermission on action

Set the permission for the action through the AuthorizationPermission attribute.

    using iCat.Authorization;
    ...

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

Obtain current user permits, claims

The IPermitClaimProcessor provides a method to obtain the logged in user's claim from the Permit. <br> The IPermissionProvider provides all permits definition.

    using iCat.Authorization;
    using iCat.Authorization.Utilities;
   [ApiController]
   [Route("[controller]")]
   public class TestController : ControllerBase
   {
        private readonly IPermitClaimProcessor _permitClaimProcessor;
        private readonly IPermissionProvider _permissionProvider;

       public TestController(IPermitClaimProcessor permitClaimeProcessor, IPermissionProvider permissionProvider)
       {
            _permitClaimProcessor = permitClaimeProcessor ?? throw new ArgumentNullException(nameof(permitProvider));
            _permissionProvider = permissionProvider ?? throw new ArgumentNullException(nameof(permissionProvider));
       }
       
       [AuthorizationPermissions(
            DepartmentPermission.Read | DepartmentPermission.Delete,
            UserProfilePermission.Add | UserProfilePermission.Edit | UserProfilePermission.ReadPartialDetail)]
       [HttpGet("[action]")]
       public IActionResult GetData()
       {
            var claim = _permitClaimProcessor.GeneratePermitClaim(new Permit
            {
                Value = (int)PermitEnum.UserProfile,
                PermissionsData = new List<Permission> { new Permission
                {
                    Value = (int)UserProfilePermission.Add,
                }}
            });

            var permits = _permissionProvider.GetDefinitions();
            var userPermits = _permitClaimProcessor.GetPermit();
            return Ok(userPermits);
       }
   }
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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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
1.3.4 118 1/24/2024
1.2.3 101 1/16/2024
1.2.2 87 1/16/2024
1.1.2 83 1/15/2024
1.0.2 130 1/8/2024
1.0.0 115 1/7/2024

Version 1.3.4