RA.Utilities.Authorization 10.0.0

Prefix Reserved
dotnet add package RA.Utilities.Authorization --version 10.0.0
                    
NuGet\Install-Package RA.Utilities.Authorization -Version 10.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="RA.Utilities.Authorization" Version="10.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="RA.Utilities.Authorization" Version="10.0.0" />
                    
Directory.Packages.props
<PackageReference Include="RA.Utilities.Authorization" />
                    
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 RA.Utilities.Authorization --version 10.0.0
                    
#r "nuget: RA.Utilities.Authorization, 10.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.
#:package RA.Utilities.Authorization@10.0.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=RA.Utilities.Authorization&version=10.0.0
                    
Install as a Cake Addin
#tool nuget:?package=RA.Utilities.Authorization&version=10.0.0
                    
Install as a Cake Tool

RA.Utilities.Authorization

NuGet version Codecov NuGet Downloads Documentation GitHub license

A utility library to simplify permission-based authorization in ASP.NET Core applications. This package provides a clean and flexible way to define and enforce authorization policies based on permissions associated with a user's identity.

It solves the problem of hardcoding roles and policies directly in your application code. Instead of scattering [Authorize(Roles = "Admin")] attributes, you can define granular permissions and check for them dynamically. This makes your authorization logic more maintainable, testable, and easier to manage as your application grows.

Getting started

Install the package via the .NET CLI:

dotnet add package RA.Utilities.Authorization

Or through the NuGet Package Manager in Visual Studio.

🔗 Dependencies

Prerequisites

This package is designed to work on top of an existing authentication setup. It expects that the user's identity (ClaimsPrincipal) has been populated with claims. It works seamlessly with RA.Utilities.Authentication.JwtBearer, which helps configure JWT-based authentication.

Your JWTs should contain a "permissions" claim that holds the permissions assigned to the user.

Example JWT Payload:

{
  "sub": "12345",
  "name": "John Doe",
  "permissions": [
    "products:read",
    "products:create"
  ]
}

Usage

The core of this package is the AddPermissionAuthorization() extension method and the HasPermission attribute.

1. Define Your Permissions

It's good practice to define your permissions as constants to avoid magic strings.

// Permissions/ProductPermissions.cs

public static class ProductPermissions
{
    public const string Read = "products:read";
    public const string Create = "products:create";
    public const string Update = "products:update";
    public const string Delete = "products:delete";
}

2. Register Authorization Services

In your Program.cs, call the AddPermissionAuthorization() extension method to register the necessary services and configure the authorization policies.

// Program.cs

using RA.Utilities.Authorization.Extensions;

var builder = WebApplication.CreateBuilder(args);

// Assumes you have authentication configured, e.g., using RA.Utilities.Authentication.JwtBearer
// builder.Services.AddJwtBearerAuthentication(builder.Configuration);

// 1. Add permission-based authorization services.
builder.Services.AddPermissionAuthorization();

builder.Services.AddControllers();

var app = builder.Build();

// 2. Add authentication and authorization middleware.
app.UseAuthentication();
app.UseAuthorization();

app.MapControllers();

app.Run();

3. Protect Your Endpoints

Use the [HasPermission] attribute on your controller actions or minimal API endpoints to enforce permission checks.

Controller-based Example
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using RA.Utilities.Authorization.Attributes;

[ApiController]
[Route("api/[controller]")]
[Authorize] // Ensures the user is authenticated before checking permissions
public class ProductsController : ControllerBase
{
    [HttpGet]
    [HasPermission(ProductPermissions.Read)]
    public IActionResult GetProducts()
    {
        return Ok("List of all products.");
    }

    [HttpPost]
    [HasPermission(ProductPermissions.Create)]
    public IActionResult CreateProduct()
    {
        return Created("api/products/1", "Product created.");
    }

    [HttpDelete("{id}")]
    [HasPermission(ProductPermissions.Delete)]
    public IActionResult DeleteProduct(int id)
    {
        return NoContent();
    }
}
Minimal API Example

You can also use the attribute with minimal APIs.

using RA.Utilities.Authorization.Attributes;

app.MapGet("/dashboard", () => "Sensitive dashboard data.")
   .RequireAuthorization() // Ensures the user is authenticated
   .WithMetadata(new HasPermissionAttribute("dashboard:view"));

If a user tries to access an endpoint without the required permission, the middleware will automatically return an HTTP 403 Forbidden response.

Additional documentation

For more information on how this package fits into the larger RA.Utilities ecosystem, please see the main repository documentation.

  • To learn about setting up JWT authentication, see the RA.Utilities.Authentication.JwtBearer package documentation.
  • For details on standardized API responses, refer to the RA.Utilities.Api.Results package.

Feedback

Contributions are welcome! If you have a suggestion, find a bug, or want to provide feedback, please open an issue in the RA.Utilities GitHub repository.

Pull Request Process

  1. Fork the Repository: Start by forking the RA.Utilities repository.
  2. Create a Branch: Create a new branch for your feature or bug fix from the main branch. Please use a descriptive name (e.g., feature/add-policy-provider or fix/permission-claim-type).
  3. Make Your Changes: Write your code, ensuring it adheres to the existing coding style. Add or update XML documentation for any new public APIs.
  4. Update README: If you are adding new functionality, please update the README.md file accordingly.
  5. Submit a Pull Request: Push your branch to your fork and open a pull request to the main branch of the original repository. Provide a clear description of the changes you have made.

Coding Standards

  • Follow the existing coding style and conventions used in the project.
  • Ensure all public members are documented with clear XML comments.
  • Keep changes focused. A pull request should address a single feature or bug.

Thank you for contributing!

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  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
10.0.0 187 11/24/2025
10.0.0-rc.2 157 10/30/2025