Overview
This page assumes you have read the AdminUI installation documentation area.
The AdminUI NuGet package uses a factory abstraction for creating database connections. When using the AdminUI NuGet package in your web applications, you can use the default implementation. This uses connection strings in configuration to create the connection. The connection strings it uses are in appSettings.json and cover 3 different areas of functionality:
"IdentityConnectionString": "...",
"OpenIddictConnectionString": "...",
"DataProtectionConnectionString": "...",
You can get away with just using the first 2 as the last one will default to the second one. See here for more details.
Alternatively, you can write your own connection factory implementation to create connections using a different strategy. This opens up AdminUI to users who may have not been able to take advantage of it in the past - perhaps users who deploy AdminUI in Azure and authenticates with the database via an Azure AD token.
The interface
The interface is IOpenIddictDbConnectionFactory
and it is initialized in AdminUI's service layer with the default implementation. It is defined thus:
public interface IOpenIddictDbConnectionFactory
{
// Create connection for the Identity database (Users, Claim Types, Roles etc.)
public DbConnection CreateIdentityConnection();
// Create connection for the OpenIddict database (Applications, Scopes, Authorizations and Tokens)
public DbConnection CreateOpenIddictConnection();
// Creates connection for the DataProtectionKey DbContext
public DbConnection CreateDataProtectionConnection(e);
}
To change the default implementation, simply implement this interface and tell AdminUI to use it instead of the default.
Starting point
To get started, take the result of the NuGet Installation and update the service registration to the following:
builder.Services
.AddAdminUI(
options =>
{
options.DatabaseConnectionFactoryType = DatabaseConnectionFactoryType.Custom;
})
.WithConnectionFactory<MyCustomConnectionFactory>();
The WithConnectionFactory<MyCustomConnectionFactory>()
method replaces the default connection factory implementation with that defined by MyCustomConnectionFactory
class that you write.
That's it!
Additional Considerations
If the custom connection factory has additional interfaces injected into its constructor that are unknown to AdminUI then these will have to be registered with the DI container in the usual way using builder.Services
, for example, if your custom connection factory implementation looks like this:
public class MyCustomConnectionFactory : IOpenIddictDbConnectionFactory
{
public MyCustomConnectionFactory(IConfiguration configuration, IMyOtherInterface myOtherInterface)
{
...
}
...
}
then the service registration might look like this:
builder.Services.AddSingleton<IMyOtherInterface, MyOtherInterfaceImplementation>();
builder.Services
.AddAdminUI(
options =>
{
options.DatabaseConnectionFactoryType = DatabaseConnectionFactoryType.Custom;
})
.WithConnectionFactory<MyCustomConnectionFactory>();
Sample code
A sample of how to replace the connection factory can be found on our GitHub in the CustomDatabaseConnection
project. Remember to fill in the CustomConnectionFactoryConnectionString
connection string and the license key in appSettings.json
.