Tolitech.Application.Authorization 1.0.0-preview.1

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

// Install Tolitech.Application.Authorization as a Cake Tool
#tool nuget:?package=Tolitech.Application.Authorization&version=1.0.0-preview.1&prerelease                

Tolitech.CleanArchitecture.Application.Authorization

Overview

Tolitech.CleanArchitecture.Application.Authorization is a lightweight .NET library designed to facilitate the definition and retrieval of application permissions using marker interfaces and reflection. This library helps you manage permissions in a clean and organized manner by leveraging marker interfaces to identify classes that contain permission definitions.

Features

  • Marker Interface for Permissions: Use the IPermissionDefinitions interface to mark classes that define permissions.

  • Reflection-Based Retrieval: Retrieve all permission definitions from specified assemblies using reflection.

  • Easy Integration: Seamlessly integrate with your existing .NET applications.

Getting Started

Installation

You can install the package via NuGet:

dotnet add package Tolitech.CleanArchitecture.Application.Authorization

Usage

Define Permissions

Create classes that implement the IPermissionDefinitions interface to define your permissions. For example:

namespace MyApplication.Permissions
{
    public class UserPermissions : IPermissionDefinitions
    {
        public const string ViewUsers = "Permissions.Users.View";
        public const string EditUsers = "Permissions.Users.Edit";
    }

    public class AdminPermissions : IPermissionDefinitions
    {
        public const string ManageRoles = "Permissions.Admin.ManageRoles";
        public const string ViewLogs = "Permissions.Admin.ViewLogs";
    }
}

Retrieve Permissions

Use the PermissionDefinitions.GetAllPermissions method to retrieve all permissions defined in the specified assembly:

using System.Reflection;
using Tolitech.CleanArchitecture.Application.Authorization;

Assembly assembly = Assembly.GetExecutingAssembly();
ReadOnlyCollection<string> permissions = PermissionDefinitions.GetAllPermissions(assembly);

foreach (var permission in permissions)
{
    Console.WriteLine(permission);
}

This will output:

Permissions.Users.View
Permissions.Users.Edit
Permissions.Admin.ManageRoles
Permissions.Admin.ViewLogs

Detailed Explanation

Marker Interface

The IPermissionDefinitions interface serves as a marker to identify classes that contain permission definitions. This makes it easy to locate and process permission-related classes within your application.

Reflection-Based Retrieval

The PermissionDefinitions.GetAllPermissions method uses reflection to find all classes that implement the IPermissionDefinitions interface within a given assembly. It then retrieves the values of all public static string fields within those classes, which are treated as permission names.

Benefits

  • Organized Permission Management: Keeps your permission definitions organized and easily accessible.
  • Reduced Hardcoding: Centralizes permission definitions, reducing the need to hardcode permission strings throughout your application.
  • Scalability: Easily add new permissions by simply adding new classes or fields implementing the IPermissionDefinitions interface.
Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net9.0

    • No dependencies.

NuGet packages (1)

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

Package Downloads
Tolitech.Infrastructure.Authorization

The library provides implementations for managing authorization logic. It includes classes and utilities for handling permissions, attribute-based authorization, and configuring authorization policies.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0-preview.1 44 12/12/2024