iCat.Authorization.Web 1.0.0

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

// Install iCat.Authorization.Web as a Cake Tool
#tool nuget:?package=iCat.Authorization.Web&version=1.0.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 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 permissions of the permit .
    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 .AddWebAuthorizationPermission(typeof(PermitEnum)) method, add a requirment to the policies via .AddAuthorizationPermissionRequirment().<br> iCat.Authorization.Web needs to use IHttpContextAccessor to obtain the current requested permits/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>()
            .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 AuthorizationPermissions attribute.

    using iCat.Authorization.Web;
    ...

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

Obtain current user permits, claims

The IPermitProvider provides methods to obtain the logged user's claim from the Permit. <br>

    using iCat.Authorization.Web;
   [ApiController]
   [Route("[controller]")]
   public class TestController : ControllerBase
   {
        private readonly IPermitProvider _permitProvider;

       public TestController(IPermitProvider permitProvider, IPermissionProvider permissionProvider)
       {
            _permitProvider = permitProvider ?? throw new ArgumentNullException(nameof(permitProvider));
       }
       
       [AuthorizationPermissions(
            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"),
                _permitProvider.GenerateClaim(UserProfilePermission.Add | UserProfilePermission.ReadAllDetail),
                _permitProvider.GenerateClaim(DepartmentPermission.Delete),
            };

            var userPermits = _permitProvider.GetCurrentUserPermits();
            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.0.0 60 6/20/2024

Version 1.0.0