/* * Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0) * See https://github.com/openiddict/openiddict-core for more information concerning * the license and the contributors participating to this project. */ using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.Options; using OpenIddict.Server; using OpenIddict.Server.AspNetCore; namespace Microsoft.Extensions.DependencyInjection; /// /// Exposes extensions allowing to register the OpenIddict server services. /// public static class OpenIddictServerAspNetCoreExtensions { /// /// Registers the OpenIddict server services for ASP.NET Core in the DI container. /// /// The services builder used by OpenIddict to register new services. /// This extension can be safely called multiple times. /// The instance. public static OpenIddictServerAspNetCoreBuilder UseAspNetCore(this OpenIddictServerBuilder builder) { if (builder is null) { throw new ArgumentNullException(nameof(builder)); } builder.Services.AddAuthentication(); builder.Services.TryAddScoped(); // Register the built-in event handlers used by the OpenIddict ASP.NET Core server components. // Note: the order used here is not important, as the actual order is set in the options. builder.Services.TryAdd(OpenIddictServerAspNetCoreHandlers.DefaultHandlers.Select(descriptor => descriptor.ServiceDescriptor)); // Register the built-in filters used by the default OpenIddict ASP.NET Core server event handlers. builder.Services.TryAddSingleton(); builder.Services.TryAddSingleton(); builder.Services.TryAddSingleton(); builder.Services.TryAddSingleton(); builder.Services.TryAddSingleton(); builder.Services.TryAddSingleton(); builder.Services.TryAddSingleton(); builder.Services.TryAddSingleton(); builder.Services.TryAddSingleton(); builder.Services.TryAddSingleton(); builder.Services.TryAddSingleton(); // Register the option initializer used by the OpenIddict ASP.NET Core server integration services. // Note: TryAddEnumerable() is used here to ensure the initializers are only registered once. builder.Services.TryAddEnumerable(new[] { ServiceDescriptor.Singleton, OpenIddictServerAspNetCoreConfiguration>(), ServiceDescriptor.Singleton, OpenIddictServerAspNetCoreConfiguration>(), ServiceDescriptor.Singleton, OpenIddictServerAspNetCoreConfiguration>(), ServiceDescriptor.Singleton, OpenIddictServerAspNetCoreConfiguration>() }); return new OpenIddictServerAspNetCoreBuilder(builder.Services); } /// /// Registers the OpenIddict server services for ASP.NET Core in the DI container. /// /// The services builder used by OpenIddict to register new services. /// The configuration delegate used to configure the server services. /// This extension can be safely called multiple times. /// The instance. public static OpenIddictServerBuilder UseAspNetCore( this OpenIddictServerBuilder builder, Action configuration) { if (builder is null) { throw new ArgumentNullException(nameof(builder)); } if (configuration is null) { throw new ArgumentNullException(nameof(configuration)); } configuration(builder.UseAspNetCore()); return builder; } }