Class AuthorizationServiceCollectionExtensions
- Namespace
- Microsoft.Extensions.DependencyInjection
- Assembly
- Stratara.Mediator.dll
DI extensions that wrap IMediator with an authorization decorator.
public static class AuthorizationServiceCollectionExtensions
- Inheritance
-
AuthorizationServiceCollectionExtensions
- Inherited Members
Methods
AddAuthorizingMediator<TAuthorizationProvider>(IServiceCollection)
Register TAuthorizationProvider as the
IAuthorizationProvider implementation and wire IMediator to
dispatch through an authorizing decorator that enforces
RequireRoleAttribute annotations on the request type before delegating to the
inner mediator.
public static IServiceCollection AddAuthorizingMediator<TAuthorizationProvider>(this IServiceCollection services) where TAuthorizationProvider : class, IAuthorizationProvider
Parameters
servicesIServiceCollectionThe service collection to mutate.
Returns
- IServiceCollection
The same service collection, to enable chaining.
Type Parameters
TAuthorizationProviderThe concrete provider, e.g. a service that reads roles from
HttpContext.User.
Examples
Without this, a host that declares [RequirePermission] fails at start-up rather than
letting a guarded request through unchecked:
services.AddMediator();
services.AddAuthorizingMediator<MembershipAuthorizationProvider>();
Remarks
Use this instead of AddMediator(IServiceCollection) when
command/query types may carry [RequireRole("...")] or
[RequirePermission("...")] guards. Multiple attributes of either kind on the same
request are ANDed — every role and every permission must match. Permission enforcement
additionally requires a registered IPermissionResolver and an ambient
session (ISessionContextProvider), both resolved optionally so role-only hosts are
unaffected; a permission-guarded type without a resolver fails fast at startup.
The registered IMediator instance implements IAuthorizingMediator; the startup-time validator wired by AddMediator(IServiceCollection) recognises this marker and accepts the configuration. If you further wrap this mediator with a custom decorator, have the outermost decorator also implement IAuthorizingMediator.
As with AddMediator(IServiceCollection), no telemetry
registration is required: a host-supplied Tracer is used where present, and the
framework's activity source carries the dispatch spans otherwise.