Both QuickstartIdentityServer projects samples follow similar approach to use MongoDB for configuration data.
- IdentityServer4 configuration to use mongo as repository is based on DI and IdentityServerBuilderExtensions - Similar to
8_EntityFrameworkStorage sample implementation - Hence we perform all "initial plumbing" in ConfigureServices() method at startup.cs.
public void ConfigureServices(IServiceCollection services) { ... // --- configure identity server with MONGO Repository for stores, keys, clients and scopes --- services.AddIdentityServer() .AddTemporarySigningCredential() .AddMongoRepository() .AddClients() .AddIdentityApiResources() .AddPersistedGrants() .AddTestUsers(Config.GetUsers()); ... }
-
In the IdentityServerBuilderExtensions.cs extension hold methods to enable DI related work executed by ConfigureServices() as listed above. It's important to highlight that AddMongoRepository() method configure a simple Repository Design Pattern to handle persistence to MongoDB. IdentityServerBuilderExtensions.cs configure DI to use custom implementation for required interfaces such as IClientStore, IResourceStore and IPersistedGrantStore enabling IdentityServer4 to interact with MongoDB seamlessly.
-
After that we just need to make sure all required IdentityServer4 Interfaces, listed above, are implemented leveraging MongoRepository. e.g. :
public class CustomResourceStore : IResourceStore { ... private IEnumerable GetAllApiResources() { return _dbRepository.All(); } ... }