2021-06-30 01:24:05 +08:00
|
|
|
# MongoDB integration
|
|
|
|
|
2022-01-12 00:28:52 +08:00
|
|
|
## Basic configuration
|
|
|
|
|
2021-06-30 01:24:05 +08:00
|
|
|
To configure OpenIddict to use MongoDB as the database for applications, authorizations, scopes and tokens, you'll need to:
|
|
|
|
- **Reference the `OpenIddict.MongoDb` package**:
|
2022-01-12 00:28:52 +08:00
|
|
|
|
2021-06-30 01:24:05 +08:00
|
|
|
```xml
|
2021-08-27 19:26:12 +08:00
|
|
|
<PackageReference Include="OpenIddict.MongoDb" Version="3.1.1" />
|
2021-06-30 01:24:05 +08:00
|
|
|
```
|
|
|
|
|
|
|
|
- **Configure OpenIddict to use the MongoDB stores**:
|
2022-01-12 00:28:52 +08:00
|
|
|
|
2021-06-30 01:24:05 +08:00
|
|
|
```csharp
|
|
|
|
services.AddOpenIddict()
|
|
|
|
.AddCore(options =>
|
|
|
|
{
|
|
|
|
// Note: to use a remote server, call the MongoClient constructor overload
|
|
|
|
// that accepts a connection string or an instance of MongoClientSettings.
|
|
|
|
options.UseMongoDb()
|
|
|
|
.UseDatabase(new MongoClient().GetDatabase("openiddict"));
|
2021-07-03 21:31:27 +08:00
|
|
|
});
|
|
|
|
```
|
|
|
|
|
|
|
|
Alternatively, you can register the `IMongoDatabase` instance as a service:
|
|
|
|
|
|
|
|
```csharp
|
|
|
|
services.AddOpenIddict()
|
|
|
|
.AddCore(options =>
|
|
|
|
{
|
|
|
|
options.UseMongoDb();
|
|
|
|
});
|
|
|
|
|
|
|
|
// Note: to use a remote server, call the MongoClient constructor overload
|
|
|
|
// that accepts a connection string or an instance of MongoClientSettings.
|
|
|
|
services.AddSingleton(new MongoClient().GetDatabase("shared-database-instance"));
|
2021-06-30 01:24:05 +08:00
|
|
|
```
|
|
|
|
|
|
|
|
- **Create indexes to improve performance** (recommended): for that, you can use the following script to
|
|
|
|
initialize the database and create the indexes used by the OpenIddict entities:
|
2022-01-12 00:28:52 +08:00
|
|
|
|
2021-06-30 01:24:05 +08:00
|
|
|
```csharp
|
|
|
|
using System.Threading;
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
using MongoDB.Driver;
|
|
|
|
using OpenIddict.MongoDb;
|
|
|
|
using OpenIddict.MongoDb.Models;
|
|
|
|
|
|
|
|
var services = new ServiceCollection();
|
|
|
|
services.AddOpenIddict()
|
|
|
|
.AddCore(options => options.UseMongoDb());
|
|
|
|
|
2022-01-08 01:10:45 +08:00
|
|
|
services.AddSingleton(new MongoClient("mongodb://localhost:27017").GetDatabase("openiddict"));
|
2021-06-30 01:24:05 +08:00
|
|
|
|
|
|
|
var provider = services.BuildServiceProvider();
|
|
|
|
var context = provider.GetRequiredService<IOpenIddictMongoDbContext>();
|
2022-01-08 01:10:45 +08:00
|
|
|
var options = provider.GetRequiredService<IOptionsMonitor<OpenIddictMongoDbOptions>>().CurrentValue;
|
2021-06-30 01:24:05 +08:00
|
|
|
var database = await context.GetDatabaseAsync(CancellationToken.None);
|
|
|
|
|
2022-01-08 01:10:45 +08:00
|
|
|
var applications = database.GetCollection<OpenIddictMongoDbApplication>(options.ApplicationsCollectionName);
|
2021-06-30 01:24:05 +08:00
|
|
|
|
|
|
|
await applications.Indexes.CreateManyAsync(new[]
|
|
|
|
{
|
|
|
|
new CreateIndexModel<OpenIddictMongoDbApplication>(
|
2022-01-08 01:10:45 +08:00
|
|
|
Builders<OpenIddictMongoDbApplication>.IndexKeys.Ascending(application => application.ClientId),
|
2021-06-30 01:24:05 +08:00
|
|
|
new CreateIndexOptions
|
|
|
|
{
|
|
|
|
Unique = true
|
|
|
|
}),
|
|
|
|
|
|
|
|
new CreateIndexModel<OpenIddictMongoDbApplication>(
|
2022-01-08 01:10:45 +08:00
|
|
|
Builders<OpenIddictMongoDbApplication>.IndexKeys.Ascending(application => application.PostLogoutRedirectUris),
|
2021-06-30 01:24:05 +08:00
|
|
|
new CreateIndexOptions
|
|
|
|
{
|
|
|
|
Background = true
|
|
|
|
}),
|
|
|
|
|
|
|
|
new CreateIndexModel<OpenIddictMongoDbApplication>(
|
2022-01-08 01:10:45 +08:00
|
|
|
Builders<OpenIddictMongoDbApplication>.IndexKeys.Ascending(application => application.RedirectUris),
|
2021-06-30 01:24:05 +08:00
|
|
|
new CreateIndexOptions
|
|
|
|
{
|
|
|
|
Background = true
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2022-01-08 01:10:45 +08:00
|
|
|
var authorizations = database.GetCollection<OpenIddictMongoDbAuthorization>(options.AuthorizationsCollectionName);
|
2021-06-30 01:24:05 +08:00
|
|
|
|
|
|
|
await authorizations.Indexes.CreateOneAsync(
|
|
|
|
new CreateIndexModel<OpenIddictMongoDbAuthorization>(
|
|
|
|
Builders<OpenIddictMongoDbAuthorization>.IndexKeys
|
|
|
|
.Ascending(authorization => authorization.ApplicationId)
|
|
|
|
.Ascending(authorization => authorization.Scopes)
|
|
|
|
.Ascending(authorization => authorization.Status)
|
|
|
|
.Ascending(authorization => authorization.Subject)
|
|
|
|
.Ascending(authorization => authorization.Type),
|
|
|
|
new CreateIndexOptions
|
|
|
|
{
|
|
|
|
Background = true
|
|
|
|
}));
|
|
|
|
|
2022-01-08 01:10:45 +08:00
|
|
|
var scopes = database.GetCollection<OpenIddictMongoDbScope>(options.ScopesCollectionName);
|
2021-06-30 01:24:05 +08:00
|
|
|
|
|
|
|
await scopes.Indexes.CreateOneAsync(new CreateIndexModel<OpenIddictMongoDbScope>(
|
|
|
|
Builders<OpenIddictMongoDbScope>.IndexKeys.Ascending(scope => scope.Name),
|
|
|
|
new CreateIndexOptions
|
|
|
|
{
|
|
|
|
Unique = true
|
|
|
|
}));
|
|
|
|
|
2022-01-08 01:10:45 +08:00
|
|
|
var tokens = database.GetCollection<OpenIddictMongoDbToken>(options.TokensCollectionName);
|
2021-06-30 01:24:05 +08:00
|
|
|
|
|
|
|
await tokens.Indexes.CreateManyAsync(new[]
|
|
|
|
{
|
|
|
|
new CreateIndexModel<OpenIddictMongoDbToken>(
|
2022-01-08 01:10:45 +08:00
|
|
|
Builders<OpenIddictMongoDbToken>.IndexKeys.Ascending(token => token.ReferenceId),
|
2021-06-30 01:24:05 +08:00
|
|
|
new CreateIndexOptions<OpenIddictMongoDbToken>
|
|
|
|
{
|
|
|
|
// Note: partial filter expressions are not supported on Azure Cosmos DB.
|
|
|
|
// As a workaround, the expression and the unique constraint can be removed.
|
2022-01-12 00:28:52 +08:00
|
|
|
PartialFilterExpression = Builders<OpenIddictMongoDbToken>.Filter.Exists(token => token.ReferenceId),
|
2021-06-30 01:24:05 +08:00
|
|
|
Unique = true
|
|
|
|
}),
|
|
|
|
|
|
|
|
new CreateIndexModel<OpenIddictMongoDbToken>(
|
|
|
|
Builders<OpenIddictMongoDbToken>.IndexKeys
|
|
|
|
.Ascending(token => token.ApplicationId)
|
|
|
|
.Ascending(token => token.Status)
|
|
|
|
.Ascending(token => token.Subject)
|
|
|
|
.Ascending(token => token.Type),
|
|
|
|
new CreateIndexOptions
|
|
|
|
{
|
|
|
|
Background = true
|
|
|
|
})
|
|
|
|
});
|
2022-01-12 00:28:52 +08:00
|
|
|
```
|
|
|
|
|
|
|
|
## Advanced configuration
|
|
|
|
|
|
|
|
### Use custom entities
|
|
|
|
|
|
|
|
For applications that require storing additional data alongside the properties used by OpenIddict, custom entities can be used. For that, you need to:
|
|
|
|
- **Create custom entities**:
|
|
|
|
|
|
|
|
```csharp
|
|
|
|
public class CustomApplication : OpenIddictMongoDbApplication
|
|
|
|
{
|
|
|
|
public string CustomProperty { get; set; }
|
|
|
|
}
|
|
|
|
|
|
|
|
public class CustomAuthorization : OpenIddictMongoDbAuthorization
|
|
|
|
{
|
|
|
|
public string CustomProperty { get; set; }
|
|
|
|
}
|
|
|
|
|
|
|
|
public class CustomScope : OpenIddictMongoDbScope
|
|
|
|
{
|
|
|
|
public string CustomProperty { get; set; }
|
|
|
|
}
|
|
|
|
|
|
|
|
public class CustomToken : OpenIddictMongoDbToken
|
|
|
|
{
|
|
|
|
public string CustomProperty { get; set; }
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
- **Configure MongoDb to use the custom entities**:
|
|
|
|
|
|
|
|
```csharp
|
|
|
|
services.AddOpenIddict()
|
|
|
|
.AddCore(options =>
|
|
|
|
{
|
|
|
|
options.UseMongoDb()
|
|
|
|
.ReplaceDefaultApplicationEntity<CustomApplication>()
|
|
|
|
.ReplaceDefaultAuthorizationEntity<CustomAuthorization>()
|
|
|
|
.ReplaceDefaultScopeEntity<CustomScope>()
|
|
|
|
.ReplaceDefaultTokenEntity<CustomToken>();
|
|
|
|
});
|
|
|
|
```
|
|
|
|
|
|
|
|
### Use custom collection names
|
|
|
|
|
|
|
|
By default, OpenIddict uses the `openiddict.[entity name]s` pattern to determine the default collection names.
|
|
|
|
Applications that require using different collection names can use the `Set*CollectionName()` helpers:
|
|
|
|
|
|
|
|
```csharp
|
|
|
|
services.AddOpenIddict()
|
|
|
|
.AddCore(options =>
|
|
|
|
{
|
|
|
|
options.UseMongoDb()
|
|
|
|
.SetApplicationsCollectionName("custom-applications-collection")
|
|
|
|
.SetAuthorizationsCollectionName("custom-authorizations-collection")
|
|
|
|
.SetScopesCollectionName("custom-scopes-collection")
|
|
|
|
.SetTokensCollectionName("custom-tokens-collection");
|
|
|
|
});
|
|
|
|
```
|