Configuration Data
Configuration data models the information for Clients and Resources.
Stores
Section titled “Stores”Store interfaces are designed to abstract accessing the configuration data. The stores used in Duende IdentityServer are:
- Client store for
Clientdata. - CORS policy service
for CORS support. Given that this is so closely tied to the
Clientconfiguration data, the CORS policy service is considered one of the configuration stores. - Resource store for
IdentityResource,ApiResource, andApiScopedata. - Identity Provider store for
IdentityProviderdata.
Registering Custom Stores
Section titled “Registering Custom Stores”Custom implementations of the stores must be registered in the ASP.NET Core service provider. There are convenience methods for registering these. For example:
builder.Services.AddIdentityServer() .AddClientStore<YourCustomClientStore>() .AddCorsPolicyService<YourCustomCorsPolicyService>() .AddResourceStore<YourCustomResourceStore>() .AddIdentityProviderStore<YourCustomIdentityProviderStore>();Caching Configuration Data
Section titled “Caching Configuration Data”Configuration data is used frequently during request processing. If this data is loaded from a database or other external store, then it might be expensive to frequently re-load the same data.
Duende IdentityServer provides convenience methods to
enable caching data from the various stores.
The caching implementation is built on Microsoft’s HybridCache from the Microsoft.Extensions.Caching.Hybrid package, registered as a keyed service under ServiceProviderKeys.ConfigurationStoreCache. For example:
builder.Services.AddIdentityServer() .AddClientStore<YourCustomClientStore>() .AddCorsPolicyService<YourCustomCorsPolicyService>() .AddResourceStore<YourCustomResourceStore>() .AddInMemoryCaching() .AddClientStoreCache<YourCustomClientStore>() .AddCorsPolicyCache<YourCustomCorsPolicyService>() .AddResourceStoreCache<YourCustomResourceStore>() .AddIdentityProviderStoreCache<YourCustomIdentityProviderStore>();For Entity Framework users, there is a convenience method AddConfigurationStoreCache() that enables caching for all configuration stores at once:
builder.Services.AddIdentityServer() .AddConfigurationStore(...) .AddConfigurationStoreCache();The duration of the data in the default cache is configurable on
the IdentityServerOptions.
For example:
builder.Services.AddIdentityServer(options => { options.Caching.ClientStoreExpiration = TimeSpan.FromMinutes(5); options.Caching.ResourceStoreExpiration = TimeSpan.FromMinutes(5);}) .AddClientStore<YourCustomClientStore>() .AddCorsPolicyService<YourCustomCorsPolicyService>() .AddResourceStore<YourCustomResourceStore>() .AddInMemoryCaching() .AddClientStoreCache<YourCustomClientStore>() .AddCorsPolicyCache<YourCustomCorsPolicyService>() .AddResourceStoreCache<YourCustomResourceStore>();Further customization of the cache is possible:
- The caching stores use a keyed
HybridCacheinstance registered underServiceProviderKeys.ConfigurationStoreCache. You can customize theHybridCachebehavior by configuring the keyed service registration (e.g., adding a distributed cache backend viaIDistributedCache). - By default, only the L1 (in-memory) cache tier is used. To enable L2 (distributed) caching, register an
IDistributedCacheimplementation (e.g., Redis viaAddStackExchangeRedisCache).HybridCachewill automatically use it as the L2 tier.
Duende IdentityServer provides convenience methods to
enable caching data from the various stores.
The caching implementation relies upon an ICache<T> service and must also be added to the ASP.NET Core service provider.
For example:
builder.Services.AddIdentityServer() .AddClientStore<YourCustomClientStore>() .AddCorsPolicyService<YourCustomCorsPolicyService>() .AddResourceStore<YourCustomResourceStore>() .AddInMemoryCaching() .AddClientStoreCache<YourCustomClientStore>() .AddCorsPolicyCache<YourCustomCorsPolicyService>() .AddResourceStoreCache<YourCustomResourceStore>() .AddIdentityProviderStoreCache<YourCustomIdentityProviderStore>();The duration of the data in the default cache is configurable on
the IdentityServerOptions.
For example:
builder.Services.AddIdentityServer(options => { options.Caching.ClientStoreExpiration = TimeSpan.FromMinutes(5); options.Caching.ResourceStoreExpiration = TimeSpan.FromMinutes(5);}) .AddClientStore<YourCustomClientStore>() .AddCorsPolicyService<YourCustomCorsPolicyService>() .AddResourceStore<YourCustomResourceStore>() .AddInMemoryCaching() .AddClientStoreCache<YourCustomClientStore>() .AddCorsPolicyCache<YourCustomCorsPolicyService>() .AddResourceStoreCache<YourCustomResourceStore>();Further customization of the cache is possible:
- If you wish to customize the caching behavior for the specific configuration objects, you can replace the
ICache<T>service implementation in the dependency injection system. - The default implementation of the
ICache<T>itself relies upon theIMemoryCacheinterface (andMemoryCacheimplementation) provided by .NET. If you wish to customize the in-memory caching behavior, you can replace theIMemoryCacheimplementation in the dependency injection system.
In-Memory Stores
Section titled “In-Memory Stores”IdentityServer includes in-memory store implementations for configuration data that are useful during development and testing. For full details, see In-Memory Stores.