iCat.Authorization.Web 3.1.0

dotnet add package iCat.Authorization.Web --version 3.1.0
                    
NuGet\Install-Package iCat.Authorization.Web -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.Web" Version="3.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="iCat.Authorization.Web" Version="3.1.0" />
                    
Directory.Packages.props
<PackageReference Include="iCat.Authorization.Web" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add iCat.Authorization.Web --version 3.1.0
                    
#r "nuget: iCat.Authorization.Web, 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.
#:package iCat.Authorization.Web@3.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=iCat.Authorization.Web&version=3.1.0
                    
Install as a Cake Addin
#tool nuget:?package=iCat.Authorization.Web&version=3.1.0
                    
Install as a Cake Tool

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.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.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
3.1.0 106 1/8/2025
1.0.0 150 6/20/2024

Version 3.1.0