mirror of
https://gitee.com/dcren/openiddict-documentation.git
synced 2025-07-15 14:04:34 +08:00
245 lines
9.6 KiB
HTML
245 lines
9.6 KiB
HTML
<!DOCTYPE html>
|
|
<!--[if IE]><![endif]-->
|
|
<html>
|
|
|
|
<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 ">
|
|
<meta name="generator" content="docfx 2.56.7.0">
|
|
|
|
<link rel="shortcut icon" href="../images/favicon.ico">
|
|
<link rel="stylesheet" href="../styles/docfx.vendor.css">
|
|
<link rel="stylesheet" href="../styles/docfx.css">
|
|
<link rel="stylesheet" href="../styles/main.css">
|
|
<link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet">
|
|
<meta property="docfx:navrel" content="../toc.html">
|
|
<meta property="docfx:tocrel" content="toc.html">
|
|
|
|
|
|
|
|
</head> <body data-spy="scroll" data-target="#affix" data-offset="120">
|
|
<div id="wrapper">
|
|
<header>
|
|
|
|
<nav id="autocollapse" class="navbar navbar-inverse ng-scope" role="navigation">
|
|
<div class="container">
|
|
<div class="navbar-header">
|
|
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navbar">
|
|
<span class="sr-only">Toggle navigation</span>
|
|
<span class="icon-bar"></span>
|
|
<span class="icon-bar"></span>
|
|
<span class="icon-bar"></span>
|
|
</button>
|
|
|
|
<a class="navbar-brand" href="../index.html">
|
|
<img id="logo" class="svg" src="../images/logo.png" alt="">
|
|
</a> </div>
|
|
<div class="collapse navbar-collapse" id="navbar">
|
|
<form class="navbar-form navbar-right" role="search" id="search">
|
|
<div class="form-group">
|
|
<input type="text" class="form-control" id="search-query" placeholder="Search" autocomplete="off">
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</nav>
|
|
|
|
<div class="subnav navbar navbar-default">
|
|
<div class="container hide-when-search" id="breadcrumb">
|
|
<ul class="breadcrumb">
|
|
<li></li>
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
</header>
|
|
<div role="main" class="container body-content hide-when-search">
|
|
|
|
<div class="sidenav hide-when-search">
|
|
<a class="btn toc-toggle collapse" data-toggle="collapse" href="#sidetoggle" aria-expanded="false" aria-controls="sidetoggle">Show / Hide Table of Contents</a>
|
|
<div class="sidetoggle collapse" id="sidetoggle">
|
|
<div id="sidetoc"></div>
|
|
</div>
|
|
</div>
|
|
<div class="article row grid-right">
|
|
<div class="col-md-10">
|
|
<article class="content wrap" id="_content" data-uid="">
|
|
<h1 id="mongodb-integration">MongoDB integration</h1>
|
|
|
|
<p>To configure OpenIddict to use MongoDB as the database for applications, authorizations, scopes and tokens, you'll need to:</p>
|
|
<ul>
|
|
<li><p><strong>Reference the <code>OpenIddict.MongoDb</code> package</strong>:</p>
|
|
<pre><code class="lang-xml"><PackageReference Include="OpenIddict.MongoDb" Version="3.1.1" />
|
|
</code></pre></li>
|
|
<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"));
|
|
});
|
|
</code></pre><p>Alternatively, you can register the <code>IMongoDatabase</code> instance as a service:</p>
|
|
<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"));
|
|
</code></pre></li>
|
|
<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());
|
|
|
|
services.AddSingleton(new MongoClient(
|
|
"mongodb://localhost:27017").GetDatabase("openiddict"));
|
|
|
|
var provider = services.BuildServiceProvider();
|
|
var context = provider.GetRequiredService<IOpenIddictMongoDbContext>();
|
|
var options = provider.GetRequiredService<
|
|
IOptionsMonitor<OpenIddictMongoDbOptions>>().CurrentValue;
|
|
var database = await context.GetDatabaseAsync(CancellationToken.None);
|
|
|
|
var applications = database.GetCollection<OpenIddictMongoDbApplication>(
|
|
options.ApplicationsCollectionName);
|
|
|
|
await applications.Indexes.CreateManyAsync(new[]
|
|
{
|
|
new CreateIndexModel<OpenIddictMongoDbApplication>(
|
|
Builders<OpenIddictMongoDbApplication>.IndexKeys.Ascending(
|
|
application => application.ClientId),
|
|
new CreateIndexOptions
|
|
{
|
|
Unique = true
|
|
}),
|
|
|
|
new CreateIndexModel<OpenIddictMongoDbApplication>(
|
|
Builders<OpenIddictMongoDbApplication>.IndexKeys.Ascending(
|
|
application => application.PostLogoutRedirectUris),
|
|
new CreateIndexOptions
|
|
{
|
|
Background = true
|
|
}),
|
|
|
|
new CreateIndexModel<OpenIddictMongoDbApplication>(
|
|
Builders<OpenIddictMongoDbApplication>.IndexKeys.Ascending(
|
|
application => application.RedirectUris),
|
|
new CreateIndexOptions
|
|
{
|
|
Background = true
|
|
})
|
|
});
|
|
|
|
var authorizations = database.GetCollection<OpenIddictMongoDbAuthorization>(
|
|
options.AuthorizationsCollectionName);
|
|
|
|
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
|
|
}));
|
|
|
|
var scopes = database.GetCollection<OpenIddictMongoDbScope>(
|
|
options.ScopesCollectionName);
|
|
|
|
await scopes.Indexes.CreateOneAsync(new CreateIndexModel<OpenIddictMongoDbScope>(
|
|
Builders<OpenIddictMongoDbScope>.IndexKeys.Ascending(scope => scope.Name),
|
|
new CreateIndexOptions
|
|
{
|
|
Unique = true
|
|
}));
|
|
|
|
var tokens = database.GetCollection<OpenIddictMongoDbToken>(
|
|
options.TokensCollectionName);
|
|
|
|
await tokens.Indexes.CreateManyAsync(new[]
|
|
{
|
|
new CreateIndexModel<OpenIddictMongoDbToken>(
|
|
Builders<OpenIddictMongoDbToken>.IndexKeys.Ascending(
|
|
token => token.ReferenceId),
|
|
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.
|
|
PartialFilterExpression =
|
|
Builders<OpenIddictMongoDbToken>.Filter.Exists(
|
|
token => token.ReferenceId),
|
|
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
|
|
})
|
|
});
|
|
</code></pre></li>
|
|
</ul>
|
|
</article>
|
|
</div>
|
|
|
|
<div class="hidden-sm col-md-2" role="complementary">
|
|
<div class="sideaffix">
|
|
<div class="contribution">
|
|
<ul class="nav">
|
|
<li>
|
|
<a href="https://github.com/openiddict/openiddict-documentation/blob/dev/configuration/mongodb-integration.md/#L1" class="contribution-link">Improve this Doc</a>
|
|
</li>
|
|
</ul>
|
|
</div>
|
|
<nav class="bs-docs-sidebar hidden-print hidden-xs hidden-sm affix" id="affix">
|
|
<h5>In This Article</h5>
|
|
<div></div>
|
|
</nav>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<footer>
|
|
<div class="grad-bottom"></div>
|
|
<div class="footer">
|
|
<div class="container">
|
|
<span class="pull-right">
|
|
<a href="#top">Back to top</a>
|
|
</span>
|
|
|
|
<span>Generated by <strong>DocFX</strong></span>
|
|
</div>
|
|
</div>
|
|
</footer>
|
|
</div>
|
|
|
|
<script type="text/javascript" src="../styles/docfx.vendor.js"></script>
|
|
<script type="text/javascript" src="../styles/docfx.js"></script>
|
|
<script type="text/javascript" src="../styles/main.js"></script>
|
|
</body>
|
|
</html>
|