2022-01-08 01:11:48 +08:00
<!DOCTYPE html>
<!-- [if IE]><![endif] -->
< html >
2023-05-03 03:58:39 +08:00
2021-06-30 01:26:33 +08:00
< head >
< meta charset = "utf-8" >
< meta http-equiv = "X-UA-Compatible" content = "IE=edge,chrome=1" >
< title > MongoDB integration < / title >
< meta name = "viewport" content = "width=device-width" >
< meta name = "title" content = "MongoDB integration " >
2023-05-03 03:58:39 +08:00
< meta name = "generator" content = "docfx 2.56.7.0" >
2021-06-30 01:26:33 +08:00
< link rel = "shortcut icon" href = "../images/favicon.ico" >
2022-01-08 01:11:48 +08:00
< link href = "https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500;700&display=swap" rel = "stylesheet" >
< link rel = "stylesheet" href = "//cdnjs.cloudflare.com/ajax/libs/highlight.js/10.1.1/styles/night-owl.min.css" >
< link rel = "stylesheet" href = "../styles/colors.css" >
< link rel = "stylesheet" href = "../styles/discord.css" >
2021-06-30 01:26:33 +08:00
< link rel = "stylesheet" href = "../styles/main.css" >
< meta property = "docfx:navrel" content = "../toc.html" >
< meta property = "docfx:tocrel" content = "toc.html" >
2023-05-03 03:58:39 +08:00
2022-01-08 01:11:48 +08:00
< / head >
< body >
< div class = "top-navbar" >
< a href = "javascript:void(0);" class = "burger-icon" onclick = "toggleMenu()" >
< svg name = "Hamburger" style = "vertical-align: middle;" width = "24" height = "24" viewbox = "0 0 24 24" > < path fill = "currentColor" fill-rule = "evenodd" clip-rule = "evenodd" d = "M20 6H4V9H20V6ZM4 10.999H20V13.999H4V10.999ZM4 15.999H20V18.999H4V15.999Z" > < / path > < / svg >
< / a >
2023-05-03 03:58:39 +08:00
2022-01-08 01:11:48 +08:00
< a class = "brand" href = "../index.html" >
2022-01-08 01:58:08 +08:00
< img src = "../images/logo.png" alt = "OpenIddict" class = "logomark" >
< span class = "brand-title" > OpenIddict< / span >
2022-01-08 01:11:48 +08:00
< / a >
< / div >
< div class = "body-content" >
< div id = "blackout" class = "blackout" onclick = "toggleMenu()" > < / div >
< nav id = "sidebar" role = "navigation" >
< div class = "sidebar" >
2023-05-03 03:58:39 +08:00
2022-01-08 01:11:48 +08:00
< div >
2023-05-03 03:58:39 +08:00
2022-01-08 01:11:48 +08:00
< a class = "brand" href = "../index.html" >
2022-01-08 01:58:08 +08:00
< img src = "../images/logo.png" alt = "OpenIddict" class = "logomark" >
< span class = "brand-title" > OpenIddict< / span >
2022-01-08 01:11:48 +08:00
< / a >
< div id = "navbar" >
2023-05-03 03:58:39 +08:00
2022-01-08 01:11:48 +08:00
< / div >
2023-05-03 03:58:39 +08:00
2022-01-08 01:11:48 +08:00
< / div >
< div class = "sidebar-item-separator" > < / div >
2023-05-03 03:58:39 +08:00
2022-01-08 01:11:48 +08:00
< div id = "sidetoggle" >
< div id = "sidetoc" > < / div >
< / div >
< / div >
< div class = "footer" >
2023-05-03 03:58:39 +08:00
< span > Generated by < strong > DocFX< / strong > < / span >
2022-01-08 01:11:48 +08:00
< / div >
< / nav >
< main class = "main-panel" >
< div role = "main" class = "hide-when-search" >
2023-05-03 03:58:39 +08:00
2022-01-08 01:11:48 +08:00
< div class = "subnav navbar navbar-default" >
< div class = "container hide-when-search" id = "breadcrumb" >
< ul class = "breadcrumb" >
< li > < / li >
< / ul >
< / div >
< / div >
< article class = "content wrap" id = "_content" data-uid = "" >
2021-06-30 01:26:33 +08:00
< h1 id = "mongodb-integration" > MongoDB integration< / h1 >
2022-01-12 00:30:01 +08:00
< h2 id = "basic-configuration" > Basic configuration< / h2 >
2023-05-03 03:58:39 +08:00
< p > To configure OpenIddict to use MongoDB as the database for applications, authorizations, scopes and tokens, you' ll need to:< / p >
2021-06-30 01:26:33 +08:00
< ul >
< li > < p > < strong > Reference the < code > OpenIddict.MongoDb< / code > package< / strong > :< / p >
2023-05-26 00:46:15 +08:00
< pre > < code class = "lang-xml" > < PackageReference Include=" OpenIddict.MongoDb" Version=" 4.4.0" />
2023-05-03 03:58:39 +08:00
< / code > < / pre > < / li >
2021-06-30 01:26:33 +08:00
< li > < p > < strong > Configure OpenIddict to use the MongoDB stores< / strong > :< / p >
< pre > < code class = "lang-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:33:55 +08:00
});
2023-05-03 03:58:39 +08:00
< / code > < / pre > < p > Alternatively, you can register the < code > IMongoDatabase< / code > instance as a service:< / p >
2021-07-03 21:33:55 +08:00
< pre > < code class = "lang-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" ));
2023-05-03 03:58:39 +08:00
< / code > < / pre > < / li >
2021-06-30 01:26:33 +08:00
< li > < p > < strong > Create indexes to improve performance< / strong > (recommended): for that, you can use the following script to
initialize the database and create the indexes used by the OpenIddict entities:< / p >
< pre > < code class = "lang-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:11:48 +08:00
services.AddSingleton(new MongoClient(" mongodb://localhost:27017" ).GetDatabase(" openiddict" ));
2021-06-30 01:26:33 +08:00
var provider = services.BuildServiceProvider();
var context = provider.GetRequiredService< IOpenIddictMongoDbContext> ();
2022-01-08 01:11:48 +08:00
var options = provider.GetRequiredService< IOptionsMonitor< OpenIddictMongoDbOptions> > ().CurrentValue;
2021-06-30 01:26:33 +08:00
var database = await context.GetDatabaseAsync(CancellationToken.None);
2022-01-08 01:11:48 +08:00
var applications = database.GetCollection< OpenIddictMongoDbApplication> (options.ApplicationsCollectionName);
2021-06-30 01:26:33 +08:00
await applications.Indexes.CreateManyAsync(new[]
{
new CreateIndexModel< OpenIddictMongoDbApplication> (
2022-01-08 01:11:48 +08:00
Builders< OpenIddictMongoDbApplication> .IndexKeys.Ascending(application => application.ClientId),
2021-06-30 01:26:33 +08:00
new CreateIndexOptions
{
Unique = true
}),
new CreateIndexModel< OpenIddictMongoDbApplication> (
2022-01-08 01:11:48 +08:00
Builders< OpenIddictMongoDbApplication> .IndexKeys.Ascending(application => application.PostLogoutRedirectUris),
2021-06-30 01:26:33 +08:00
new CreateIndexOptions
{
Background = true
}),
new CreateIndexModel< OpenIddictMongoDbApplication> (
2022-01-08 01:11:48 +08:00
Builders< OpenIddictMongoDbApplication> .IndexKeys.Ascending(application => application.RedirectUris),
2021-06-30 01:26:33 +08:00
new CreateIndexOptions
{
Background = true
})
});
2022-01-08 01:11:48 +08:00
var authorizations = database.GetCollection< OpenIddictMongoDbAuthorization> (options.AuthorizationsCollectionName);
2021-06-30 01:26:33 +08:00
2021-06-30 01:30:39 +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
}));
2021-06-30 01:26:33 +08:00
2022-01-08 01:11:48 +08:00
var scopes = database.GetCollection< OpenIddictMongoDbScope> (options.ScopesCollectionName);
2021-06-30 01:26:33 +08:00
await scopes.Indexes.CreateOneAsync(new CreateIndexModel< OpenIddictMongoDbScope> (
Builders< OpenIddictMongoDbScope> .IndexKeys.Ascending(scope => scope.Name),
new CreateIndexOptions
{
Unique = true
}));
2022-01-08 01:11:48 +08:00
var tokens = database.GetCollection< OpenIddictMongoDbToken> (options.TokensCollectionName);
2021-06-30 01:26:33 +08:00
await tokens.Indexes.CreateManyAsync(new[]
{
new CreateIndexModel< OpenIddictMongoDbToken> (
2022-01-08 01:11:48 +08:00
Builders< OpenIddictMongoDbToken> .IndexKeys.Ascending(token => token.ReferenceId),
2021-06-30 01:26:33 +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:30:01 +08:00
PartialFilterExpression = Builders< OpenIddictMongoDbToken> .Filter.Exists(token => token.ReferenceId),
2021-06-30 01:26:33 +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
})
});
2023-05-03 03:58:39 +08:00
< / code > < / pre > < / li >
2021-06-30 01:26:33 +08:00
< / ul >
2022-01-12 00:30:01 +08:00
< h2 id = "advanced-configuration" > Advanced configuration< / h2 >
< h3 id = "use-custom-entities" > Use custom entities< / h3 >
< p > For applications that require storing additional data alongside the properties used by OpenIddict, custom entities can be used. For that, you need to:< / p >
< ul >
< li > < p > < strong > Create custom entities< / strong > :< / p >
< pre > < code class = "lang-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; }
}
2023-05-03 03:58:39 +08:00
< / code > < / pre > < / li >
2022-01-12 00:30:01 +08:00
< li > < p > < strong > Configure MongoDb to use the custom entities< / strong > :< / p >
< pre > < code class = "lang-csharp" > services.AddOpenIddict()
.AddCore(options =>
{
options.UseMongoDb()
.ReplaceDefaultApplicationEntity< CustomApplication> ()
.ReplaceDefaultAuthorizationEntity< CustomAuthorization> ()
.ReplaceDefaultScopeEntity< CustomScope> ()
.ReplaceDefaultTokenEntity< CustomToken> ();
});
2023-05-03 03:58:39 +08:00
< / code > < / pre > < / li >
2022-01-12 00:30:01 +08:00
< / ul >
< h3 id = "use-custom-collection-names" > Use custom collection names< / h3 >
< p > By default, OpenIddict uses the < code > openiddict.[entity name]s< / code > pattern to determine the default collection names.
Applications that require using different collection names can use the < code > Set*CollectionName()< / code > helpers:< / p >
< pre > < code class = "lang-csharp" > services.AddOpenIddict()
.AddCore(options =>
{
options.UseMongoDb()
.SetApplicationsCollectionName(" custom-applications-collection" )
.SetAuthorizationsCollectionName(" custom-authorizations-collection" )
.SetScopesCollectionName(" custom-scopes-collection" )
.SetTokensCollectionName(" custom-tokens-collection" );
});
2023-05-03 03:58:39 +08:00
< / code > < / pre > < / article >
2022-01-08 01:11:48 +08:00
< / div >
< / main >
< / div >
2023-05-03 03:58:39 +08:00
< script src = "https://code.jquery.com/jquery-3.5.1.min.js" integrity = "sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin = "anonymous" > < / script >
< script src = "https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity = "sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin = "anonymous" > < / script >
< script src = "https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" integrity = "sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" crossorigin = "anonymous" > < / script >
< script src = "//cdnjs.cloudflare.com/ajax/libs/highlight.js/10.1.1/highlight.min.js" > < / script >
< script type = "text/javascript" src = "../styles/jquery.twbsPagination.js" > < / script >
< script type = "text/javascript" src = "../styles/url.min.js" > < / script >
< script src = "https://cdn.jsdelivr.net/npm/anchor-js/anchor.min.js" > < / script >
< script type = "text/javascript" src = "../styles/docfx.js" > < / script >
< script type = "text/javascript" src = "../styles/main.js" > < / script >
2022-01-08 01:11:48 +08:00
< / body >
< / html >