Skip to main content

ServiceCollectionExtensions Class

Abblix.Jwt

Abblix.Jwt

ServiceCollectionExtensions Class

Provides extension methods for Microsoft.Extensions.DependencyInjection.IServiceCollection to register JwT-related services within the application.

public static class ServiceCollectionExtensions

Inheritance System.Object → ServiceCollectionExtensions

Methods

ServiceCollectionExtensions.AddCriticalHeaderHandler<THandler>(this IServiceCollection, string) Method

Registers an ICriticalHeaderHandler for a single JOSE header extension parameter listed in a JWS 'crit' array (RFC 7515 §4.1.11). The parameter name is the DI key, so the registration cannot claim a name without a handler behind it — name and behaviour are inseparable.

public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddCriticalHeaderHandler<THandler>(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, string headerName)
where THandler : class, Abblix.Jwt.ICriticalHeaderHandler;

Type parameters

THandler

Concrete handler type.

Parameters

services Microsoft.Extensions.DependencyInjection.IServiceCollection

The service collection to register the handler in.

headerName System.String

The JOSE header parameter name the handler implements (byte-exact per RFC 7515 §5.3); used as the DI key the validator routes a 'crit' name to. A handler covering a family of related names registers under each.

Returns

Microsoft.Extensions.DependencyInjection.IServiceCollection
The service collection for method chaining.

Remarks

Keyed-name DI mirrors the signer/encryptor registrations in this assembly (Abblix.Jwt.ServiceCollectionExtensions.AddDataSigner<>.Extensions.DependencyInjection.IServiceCollection,System.String,Abblix.Jwt.SigningAlgorithmsProvider) by 'alg'): one keyed registration serves O(1) request-time dispatch (GetKeyedService<ICriticalHeaderHandler>(name)). Microsoft.Extensions.DependencyInjection.Extensions.ServiceCollectionDescriptorExtensions.TryAddKeyedSingleton<>.Extensions.DependencyInjection.IServiceCollection,System.Object) dedups by (service, key) first-wins, so a host pre-registration for a name wins over a later default.

ServiceCollectionExtensions.AddJsonWebTokens(this IServiceCollection) Method

Registers services for creating and validating JSON Web Tokens (JWTs) within the application.

public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddJsonWebTokens(this Microsoft.Extensions.DependencyInjection.IServiceCollection services);

Parameters

services Microsoft.Extensions.DependencyInjection.IServiceCollection

The Microsoft.Extensions.DependencyInjection.IServiceCollection to configure with JWT services.

Returns

Microsoft.Extensions.DependencyInjection.IServiceCollection
The configured Microsoft.Extensions.DependencyInjection.IServiceCollection, enabling further chaining of service registrations.

Remarks

This method adds services for JWT handling, enabling the application to generate and validate JWTs efficiently. JWTs are an essential part of modern web application security, used for representing claims securely between two parties.

By registering these services, the application can: - Create JWTs with IJsonWebTokenCreator, allowing for the generation of tokens that can securely transmit information between parties. - Validate JWTs with IJsonWebTokenValidator, ensuring that incoming tokens are valid and have not been tampered with.

This setup is crucial for implementing authentication and authorization mechanisms that rely on JWTs, such as OAuth 2.0 and OpenID Connect.