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
<PackageReference Include="RGamaFelix.CqrsDispatcher.Authorization" Version="4.0.0" />
<PackageVersion Include="RGamaFelix.CqrsDispatcher.Authorization" Version="4.0.0" />
<PackageReference Include="RGamaFelix.CqrsDispatcher.Authorization" />
paket add RGamaFelix.CqrsDispatcher.Authorization --version 4.0.0
#r "nuget: RGamaFelix.CqrsDispatcher.Authorization, 4.0.0"
#:package RGamaFelix.CqrsDispatcher.Authorization@4.0.0
#addin nuget:?package=RGamaFelix.CqrsDispatcher.Authorization&version=4.0.0
#tool nuget:?package=RGamaFelix.CqrsDispatcher.Authorization&version=4.0.0
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.AuthorizationMicrosoft.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
Handlemethod is called.
| Product | Versions 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. |
-
net10.0
- Microsoft.AspNetCore.Authorization (>= 10.0.5)
- Microsoft.AspNetCore.Http.Abstractions (>= 2.3.9)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.5)
- Microsoft.Extensions.Logging (>= 10.0.5)
- Microsoft.Extensions.Logging.Abstractions (>= 10.0.5)
- RGamaFelix.CqrsDispatcher (>= 4.0.0)
-
net8.0
- Microsoft.AspNetCore.Authorization (>= 8.0.25)
- Microsoft.AspNetCore.Http.Abstractions (>= 2.3.9)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.2)
- Microsoft.Extensions.Logging (>= 8.0.1)
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.3)
- RGamaFelix.CqrsDispatcher (>= 4.0.0)
-
net9.0
- Microsoft.AspNetCore.Authorization (>= 9.0.14)
- Microsoft.AspNetCore.Http.Abstractions (>= 2.3.9)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.14)
- Microsoft.Extensions.Logging (>= 9.0.14)
- Microsoft.Extensions.Logging.Abstractions (>= 9.0.14)
- RGamaFelix.CqrsDispatcher (>= 4.0.0)
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 |