/// <summary> /// Adds API resources from the given <paramref name="configuration"/> instance. /// </summary> /// <param name="builder">The <see cref="IIdentityServerBuilder"/>.</param> /// <param name="configuration">The <see cref="IConfiguration"/> instance containing the API definitions.</param> /// <returns>The <see cref="IIdentityServerBuilder"/>.</returns> public static IIdentityServerBuilder AddApiResources( this IIdentityServerBuilder builder, IConfiguration configuration) { builder.ConfigureReplacedServices(); builder.AddApiScopes(); builder.AddInMemoryApiResources(Enumerable.Empty <ApiResource>()); builder.Services.TryAddEnumerable( ServiceDescriptor.Singleton <IConfigureOptions <ApiAuthorizationOptions>, ConfigureApiResources>(sp => { var logger = sp.GetRequiredService <ILogger <ConfigureApiResources> >(); var effectiveConfig = configuration ?? sp.GetRequiredService <IConfiguration>().GetSection("IdentityServer:Resources"); var localApiDescriptor = sp.GetService <IIdentityServerJwtDescriptor>(); return(new ConfigureApiResources(effectiveConfig, localApiDescriptor, logger)); })); // We take over the setup for the API resources as Identity Server registers the enumerable as a singleton // and that prevents normal composition. builder.Services.AddSingleton <IEnumerable <ApiResource> >(sp => { var options = sp.GetRequiredService <IOptions <ApiAuthorizationOptions> >(); return(options.Value.ApiResources); }); return(builder); }