-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIdentityServerEntityFrameworkBuilderExtensions.cs
More file actions
124 lines (110 loc) · 5.23 KB
/
IdentityServerEntityFrameworkBuilderExtensions.cs
File metadata and controls
124 lines (110 loc) · 5.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
using IdentityServer7.EntityFramework.DbContexts;
using IdentityServer7.EntityFramework.Interfaces;
using IdentityServer7.EntityFramework.Services;
using IdentityServer7.EntityFramework.Stores;
using IdentityServer7.Stores;
using System;
using IdentityServer7.EntityFramework.Options;
using IdentityServer7.EntityFramework;
using IdentityServer7.EntityFramework.Storage;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Hosting;
namespace Microsoft.Extensions.DependencyInjection
{
/// <summary>
/// Extension methods to add EF database support to IdentityServer.
/// </summary>
public static class IdentityServerEntityFrameworkBuilderExtensions
{
/// <summary>
/// Configures EF implementation of IClientStore, IResourceStore, and ICorsPolicyService with IdentityServer.
/// </summary>
/// <param name="builder">The builder.</param>
/// <param name="storeOptionsAction">The store options action.</param>
/// <returns></returns>
public static IIdentityServerBuilder AddConfigurationStore(
this IIdentityServerBuilder builder,
Action<ConfigurationStoreOptions>? storeOptionsAction = null)
{
return builder.AddConfigurationStore<ConfigurationDbContext>(storeOptionsAction);
}
/// <summary>
/// Configures EF implementation of IClientStore, IResourceStore, and ICorsPolicyService with IdentityServer.
/// </summary>
/// <typeparam name="TContext">The IConfigurationDbContext to use.</typeparam>
/// <param name="builder">The builder.</param>
/// <param name="storeOptionsAction">The store options action.</param>
/// <returns></returns>
public static IIdentityServerBuilder AddConfigurationStore<TContext>(
this IIdentityServerBuilder builder,
Action<ConfigurationStoreOptions>? storeOptionsAction = null)
where TContext : DbContext, IConfigurationDbContext
{
builder.Services.AddConfigurationDbContext<TContext>(storeOptionsAction);
builder.AddClientStore<ClientStore>();
builder.AddResourceStore<ResourceStore>();
builder.AddCorsPolicyService<CorsPolicyService>();
return builder;
}
/// <summary>
/// Configures caching for IClientStore, IResourceStore, and ICorsPolicyService with IdentityServer.
/// </summary>
/// <param name="builder">The builder.</param>
/// <returns></returns>
public static IIdentityServerBuilder AddConfigurationStoreCache(
this IIdentityServerBuilder builder)
{
builder.AddInMemoryCaching();
// add the caching decorators
builder.AddClientStoreCache<ClientStore>();
builder.AddResourceStoreCache<ResourceStore>();
builder.AddCorsPolicyCache<CorsPolicyService>();
return builder;
}
/// <summary>
/// Configures EF implementation of IPersistedGrantStore with IdentityServer.
/// </summary>
/// <param name="builder">The builder.</param>
/// <param name="storeOptionsAction">The store options action.</param>
/// <returns></returns>
public static IIdentityServerBuilder AddOperationalStore(
this IIdentityServerBuilder builder,
Action<OperationalStoreOptions>? storeOptionsAction = null)
{
return builder.AddOperationalStore<PersistedGrantDbContext>(storeOptionsAction);
}
/// <summary>
/// Configures EF implementation of IPersistedGrantStore with IdentityServer.
/// </summary>
/// <typeparam name="TContext">The IPersistedGrantDbContext to use.</typeparam>
/// <param name="builder">The builder.</param>
/// <param name="storeOptionsAction">The store options action.</param>
/// <returns></returns>
public static IIdentityServerBuilder AddOperationalStore<TContext>(
this IIdentityServerBuilder builder,
Action<OperationalStoreOptions>? storeOptionsAction = null)
where TContext : DbContext, IPersistedGrantDbContext
{
builder.Services.AddOperationalDbContext<TContext>(storeOptionsAction);
builder.Services.AddTransient<IPersistedGrantStore, PersistedGrantStore>();
builder.Services.AddTransient<IDeviceFlowStore, DeviceFlowStore>();
builder.Services.AddSingleton<IHostedService, TokenCleanupHost>();
return builder;
}
/// <summary>
/// Adds an implementation of the IOperationalStoreNotification to IdentityServer.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="builder"></param>
/// <returns></returns>
public static IIdentityServerBuilder AddOperationalStoreNotification<T>(
this IIdentityServerBuilder builder)
where T : class, IOperationalStoreNotification
{
builder.Services.AddOperationalStoreNotification<T>();
return builder;
}
}
}