This repository has been archived by the owner on Dec 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4k
/
IdentityServerServiceCollectionExtensions.cs
95 lines (84 loc) · 3.59 KB
/
IdentityServerServiceCollectionExtensions.cs
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
// 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 IdentityServer4.Configuration;
using Microsoft.Extensions.Configuration;
using System;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Options;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
namespace Microsoft.Extensions.DependencyInjection
{
/// <summary>
/// DI extension methods for adding IdentityServer
/// </summary>
public static class IdentityServerServiceCollectionExtensions
{
/// <summary>
/// Creates a builder.
/// </summary>
/// <param name="services">The services.</param>
/// <returns></returns>
public static IIdentityServerBuilder AddIdentityServerBuilder(this IServiceCollection services)
{
return new IdentityServerBuilder(services);
}
/// <summary>
/// Adds IdentityServer.
/// </summary>
/// <param name="services">The services.</param>
/// <returns></returns>
public static IIdentityServerBuilder AddIdentityServer(this IServiceCollection services)
{
var builder = services.AddIdentityServerBuilder();
builder
.AddRequiredPlatformServices()
.AddCookieAuthentication()
.AddCoreServices()
.AddDefaultEndpoints()
.AddPluggableServices()
.AddValidators()
.AddResponseGenerators()
.AddDefaultSecretParsers()
.AddDefaultSecretValidators();
// provide default in-memory implementation, not suitable for most production scenarios
builder.AddInMemoryPersistedGrants();
return builder;
}
/// <summary>
/// Adds IdentityServer.
/// </summary>
/// <param name="services">The services.</param>
/// <param name="setupAction">The setup action.</param>
/// <returns></returns>
public static IIdentityServerBuilder AddIdentityServer(this IServiceCollection services, Action<IdentityServerOptions> setupAction)
{
services.Configure(setupAction);
return services.AddIdentityServer();
}
/// <summary>
/// Adds the IdentityServer.
/// </summary>
/// <param name="services">The services.</param>
/// <param name="configuration">The configuration.</param>
/// <returns></returns>
public static IIdentityServerBuilder AddIdentityServer(this IServiceCollection services, IConfiguration configuration)
{
services.Configure<IdentityServerOptions>(configuration);
return services.AddIdentityServer();
}
/// <summary>
/// Configures the OpenIdConnect handlers to persist the state parameter into the server-side IDistributedCache.
/// </summary>
/// <param name="services">The services.</param>
/// <param name="schemes">The schemes to configure. If none provided, then all OpenIdConnect schemes will use the cache.</param>
public static IServiceCollection AddOidcStateDataFormatterCache(this IServiceCollection services, params string[] schemes)
{
services.AddSingleton<IPostConfigureOptions<OpenIdConnectOptions>>(
svcs => new ConfigureOpenIdConnectOptions(
schemes,
svcs.GetRequiredService<IHttpContextAccessor>())
);
return services;
}
}
}