RGamaFelix.CqrsDispatcher.Authorization 4.0.0

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

RGamaFelix.CqrsDispatcher.Authorization

An ASP.NET Core Authorization integration for the RGamaFelix.CqrsDispatcher framework. This package allows you to secure your command and query handlers using standard ASP.NET Core policies.

Prerequisites

  • RGamaFelix.CqrsDispatcher (Core project)
  • Microsoft.AspNetCore.Authorization
  • Microsoft.AspNetCore.Http.Abstractions

Installation

Add the authorization project to your solution and ensure it references the core RGamaFelix.CqrsDispatcher project.

# Using dotnet CLI
dotnet add package RGamaFelix.CqrsDispatcher.Authorization

Configuration

1. Register Extensions

In your Program.cs or Startup.cs, register the authorization extensions. This package provides helper methods to simplify policy creation.

using RGamaFelix.CqrsDispatcher.Authorization.Configuration;

// ... in your ServiceCollection setup
services.AddCqrsDispatcherFramework(); // Base dispatcher

// Register the authorization pipeline extensions
services.AddAuthorizationExtensions(options => {
    // Standard ASP.NET Core Authorization options
    options.AddPolicy("AdminOnly", policy => policy.RequireRole("Admin"));
});

// Optional: Use built-in helpers for common policy types
services.AddRolePolicy("Manager", "ManagerRole")
        .AddClaimPolicy("VIP", "MemberType", "VIP")
        .AddAnyScopePolicy("ReadAccess", "api.read", "api.all");

2. Configure HttpContextAccessor

Since this package relies on IHttpContextAccessor to retrieve the current user, ensure it is registered:

services.AddHttpContextAccessor();

Usage Example

1. Apply Authorization to a Handler

Use the [HandlerAuthorization] attribute on your Handler class (not the request) to specify which policy is required to execute it.

using RGamaFelix.CqrsDispatcher.Authorization;
using RGamaFelix.CqrsDispatcher.Command.Handler;

[HandlerAuthorization("AdminOnly")]
public class CreateUserHandler : ICommandHandler<CreateUserCommand>
{
    public async Task Handle(CreateUserCommand request, CancellationToken cancellationToken)
    {
        // This code only runs if the "AdminOnly" policy succeeds
        // ... implementation
    }
}

2. Handle Unauthorized Requests

If a user does not meet the policy requirements, the dispatcher will throw an UnauthorizedRequestException. You should handle this in your global exception filter or middleware.

try 
{
    await _dispatcher.PublishAsync(new CreateUserCommand(...));
}
catch (UnauthorizedRequestException ex) 
{
    // Return 403 Forbidden or 401 Unauthorized
    return Forbid();
}

Features

  • Attribute-Based Security: Secure handlers individually using [HandlerAuthorization("PolicyName")].
  • Standard Integration: Uses the standard IAuthorizationService, making it compatible with all existing ASP.NET Core authorization logic.
  • Fluent Policy Helpers: Includes extension methods for common scenarios (Roles, Claims, OAuth2 Scopes).
  • Handler-Level Granularity: Authorization is checked at the handler level, allowing different handlers for the same request type to have different security requirements.
  • Automatic Enforcement: The authorization check is part of the handler pipeline and is enforced automatically before the handler's Handle method is called.
Product Compatible and additional computed target framework versions.
.NET 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 is compatible.  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 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
4.0.0 134 5/3/2026