Class ResourceAuthorizationBehavior<TMessage, TResource, TResponse>
Pipeline behavior that loads a resource and performs resource-based authorization before the handler runs. Registered as scoped so the injected IServiceProvider is the request-scoped provider, allowing correct resolution of scoped dependencies.
public sealed class ResourceAuthorizationBehavior<TMessage, TResource, TResponse> : IPipelineBehavior<TMessage, TResponse> where TMessage : IAuthorizeResource<TResource>, IMessage where TResource : class where TResponse : IResult, IFailureFactory<TResponse>
Type Parameters
TMessageThe message type, constrained to IAuthorizeResource<TResource>.
TResourceThe resource type loaded for authorization.
TResponseThe response type, constrained to IResult and IFailureFactory<TSelf>.
- Inheritance
-
ResourceAuthorizationBehavior<TMessage, TResource, TResponse>
- Implements
-
IPipelineBehavior<TMessage, TResponse>
- Inherited Members
- Extension Methods
Remarks
This behavior cannot be registered as an open generic because it has 3 type parameters while Mediator.IPipelineBehavior<TMessage, TResponse> has 2. Register per-command via AddResourceAuthorization<TMessage, TResource, TResponse>(IServiceCollection).
The behavior is registered as scoped (not singleton) because it resolves
IResourceLoader<TMessage, TResource> from the injected IServiceProvider.
A singleton would receive the root provider, causing InvalidOperationException
when ASP.NET Core's scope validation is enabled (default in Development).
Pipeline execution order for a command implementing both IAuthorize and IAuthorizeResource<TResource>:
- AuthorizationBehavior — checks static permissions
- ResourceAuthorizationBehavior — loads resource, checks ownership
- ValidationBehavior — validates command properties
- Handler — pure business logic
Constructors
ResourceAuthorizationBehavior(IActorProvider, IServiceProvider)
Initializes a new instance of the ResourceAuthorizationBehavior<TMessage, TResource, TResponse> class.
public ResourceAuthorizationBehavior(IActorProvider actorProvider, IServiceProvider serviceProvider)
Parameters
actorProviderIActorProviderThe provider used to resolve the current actor.
serviceProviderIServiceProviderThe request-scoped service provider used to resolve the per-message resource loader.
Exceptions
- ArgumentNullException
Thrown when
actorProviderorserviceProvideris null.
ResourceAuthorizationBehavior(IActorProvider, IServiceProvider, IOptions<ResourceAuthorizationOptions>?, ILogger<ResourceAuthorizationBehavior<TMessage, TResource, TResponse>>?)
Initializes a new instance of the ResourceAuthorizationBehavior<TMessage, TResource, TResponse> class.
public ResourceAuthorizationBehavior(IActorProvider actorProvider, IServiceProvider serviceProvider, IOptions<ResourceAuthorizationOptions>? options, ILogger<ResourceAuthorizationBehavior<TMessage, TResource, TResponse>>? logger = null)
Parameters
actorProviderIActorProviderThe provider used to resolve the current actor.
serviceProviderIServiceProviderThe request-scoped service provider used to resolve the per-message resource loader.
optionsIOptions<ResourceAuthorizationOptions>Per-resource exposure-policy options resolved from DI. Null defaults to Propagate.
loggerILogger<ResourceAuthorizationBehavior<TMessage, TResource, TResponse>>Logger used to emit the
ExistenceHiddenstructured-log event when NotFound, Gone, Forbidden, or AuthenticationRequired is normalized to the public NotFound. Null defaults to Instance.
Exceptions
- ArgumentNullException
Thrown when
actorProviderorserviceProvideris null.
Methods
Handle(TMessage, MessageHandlerDelegate<TMessage, TResponse>, CancellationToken)
public ValueTask<TResponse> Handle(TMessage message, MessageHandlerDelegate<TMessage, TResponse> next, CancellationToken cancellationToken)
Parameters
messageTMessagenextMessageHandlerDelegate<TMessage, TResponse>cancellationTokenCancellationToken
Returns
- ValueTask<TResponse>