/
AzureAdB2CAuthenticationBuilderExtensions.cs
223 lines (196 loc) · 10.6 KB
/
AzureAdB2CAuthenticationBuilderExtensions.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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.using Microsoft.AspNetCore.Authorization;
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.AspNetCore.Authentication.AzureADB2C.UI;
using Microsoft.AspNetCore.Mvc.ApplicationParts;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
using System.Collections.Generic;
namespace Microsoft.AspNetCore.Authentication
{
/// <summary>
/// Extension methods to add Azure Active Directory B2C Authentication to your application.
/// </summary>
public static class AzureADB2CAuthenticationBuilderExtensions
{
/// <summary>
/// Adds JWT Bearer authentication to your app for Azure AD B2C Applications.
/// </summary>
/// <param name="builder">The <see cref="AuthenticationBuilder"/>.</param>
/// <param name="configureOptions">The <see cref="Action{AzureADB2COptions}"/> to configure the
/// <see cref="AzureADB2COptions"/>.
/// </param>
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
public static AuthenticationBuilder AddAzureADB2CBearer(this AuthenticationBuilder builder, Action<AzureADB2COptions> configureOptions) =>
builder.AddAzureADB2CBearer(
AzureADB2CDefaults.BearerAuthenticationScheme,
AzureADB2CDefaults.JwtBearerAuthenticationScheme,
configureOptions);
/// <summary>
/// Adds JWT Bearer authentication to your app for Azure AD B2C Applications.
/// </summary>
/// <param name="builder">The <see cref="AuthenticationBuilder"/>.</param>
/// <param name="scheme">The identifier for the virtual scheme.</param>
/// <param name="jwtBearerScheme">The identifier for the underlying JWT Bearer scheme.</param>
/// <param name="configureOptions">The <see cref="Action{AzureADB2COptions}"/> to configure the
/// <see cref="AzureADB2COptions"/>.
/// </param>
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
public static AuthenticationBuilder AddAzureADB2CBearer(
this AuthenticationBuilder builder,
string scheme,
string jwtBearerScheme,
Action<AzureADB2COptions> configureOptions)
{
builder.AddPolicyScheme(scheme, displayName: null, configureOptions: o =>
{
o.ForwardDefault = jwtBearerScheme;
});
builder.Services.Configure(TryAddJwtBearerSchemeMapping(scheme, jwtBearerScheme));
builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<IConfigureOptions<AzureADB2COptions>, AzureADB2COptionsConfiguration>());
builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<IConfigureOptions<JwtBearerOptions>, AzureADB2CJwtBearerOptionsConfiguration>());
builder.Services.Configure(scheme, configureOptions);
builder.AddJwtBearer(jwtBearerScheme, o => { });
return builder;
}
/// <summary>
/// Adds Azure Active Directory B2C Authentication to your application.
/// </summary>
/// <param name="builder">The <see cref="AuthenticationBuilder"/>.</param>
/// <param name="configureOptions">The <see cref="Action{AzureADB2COptions}"/> to configure the
/// <see cref="AzureADB2COptions"/>
/// </param>
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
public static AuthenticationBuilder AddAzureADB2C(this AuthenticationBuilder builder, Action<AzureADB2COptions> configureOptions) =>
builder.AddAzureADB2C(
AzureADB2CDefaults.AuthenticationScheme,
AzureADB2CDefaults.OpenIdScheme,
AzureADB2CDefaults.CookieScheme,
AzureADB2CDefaults.DisplayName,
configureOptions);
/// <summary>
/// Adds Azure Active Directory B2C Authentication to your application.
/// </summary>
/// <param name="builder">The <see cref="AuthenticationBuilder"/>.</param>
/// <param name="scheme">The identifier for the virtual scheme.</param>
/// <param name="openIdConnectScheme">The identifier for the underlying Open ID Connect scheme.</param>
/// <param name="cookieScheme">The identifier for the underlying cookie scheme.</param>
/// <param name="displayName">The display name for the scheme.</param>
/// <param name="configureOptions">The <see cref="Action{AzureADB2COptions}"/> to configure the
/// <see cref="AzureADB2COptions"/>
/// </param>
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
public static AuthenticationBuilder AddAzureADB2C(
this AuthenticationBuilder builder,
string scheme,
string openIdConnectScheme,
string cookieScheme,
string displayName,
Action<AzureADB2COptions> configureOptions)
{
AddAdditionalMvcApplicationParts(builder.Services);
builder.AddPolicyScheme(scheme, displayName, o =>
{
o.ForwardDefault = cookieScheme;
o.ForwardChallenge = openIdConnectScheme;
});
builder.Services.Configure(TryAddOpenIDCookieSchemeMappings(scheme, openIdConnectScheme, cookieScheme));
builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<IConfigureOptions<AzureADB2COptions>, AzureADB2COptionsConfiguration>());
builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<IConfigureOptions<OpenIdConnectOptions>, AzureADB2COpenIdConnectOptionsConfiguration>());
builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<IConfigureOptions<CookieAuthenticationOptions>, AzureADB2CCookieOptionsConfiguration>());
builder.Services.Configure(scheme, configureOptions);
builder.AddOpenIdConnect(openIdConnectScheme, null, o => { });
builder.AddCookie(cookieScheme, null, o => { });
return builder;
}
private static Action<AzureADB2CSchemeOptions> TryAddJwtBearerSchemeMapping(string scheme, string jwtBearerScheme)
{
return TryAddMapping;
void TryAddMapping(AzureADB2CSchemeOptions o)
{
if (o.JwtBearerMappings.ContainsKey(scheme))
{
throw new InvalidOperationException($"A scheme with the name '{scheme}' was already added.");
}
foreach (var mapping in o.JwtBearerMappings)
{
if (mapping.Value.JwtBearerScheme == jwtBearerScheme)
{
throw new InvalidOperationException(
$"The JSON Web Token Bearer scheme '{jwtBearerScheme}' can't be associated with the Azure Active Directory B2C scheme '{scheme}'. " +
$"The JSON Web Token Bearer scheme '{jwtBearerScheme}' is already mapped to the Azure Active Directory B2C scheme '{mapping.Key}'");
}
}
o.JwtBearerMappings.Add(scheme, new AzureADB2CSchemeOptions.JwtBearerSchemeMapping
{
JwtBearerScheme = jwtBearerScheme
});
};
}
private static Action<AzureADB2CSchemeOptions> TryAddOpenIDCookieSchemeMappings(string scheme, string openIdConnectScheme, string cookieScheme)
{
return TryAddMapping;
void TryAddMapping(AzureADB2CSchemeOptions o)
{
if (o.OpenIDMappings.ContainsKey(scheme))
{
throw new InvalidOperationException($"A scheme with the name '{scheme}' was already added.");
}
foreach (var mapping in o.OpenIDMappings)
{
if (mapping.Value.CookieScheme == cookieScheme)
{
throw new InvalidOperationException(
$"The cookie scheme '{cookieScheme}' can't be associated with the Azure Active Directory B2C scheme '{scheme}'. " +
$"The cookie scheme '{cookieScheme}' is already mapped to the Azure Active Directory B2C scheme '{mapping.Key}'");
}
if (mapping.Value.OpenIdConnectScheme == openIdConnectScheme)
{
throw new InvalidOperationException(
$"The Open ID Connect scheme '{openIdConnectScheme}' can't be associated with the Azure Active Directory B2C scheme '{scheme}'. " +
$"The Open ID Connect scheme '{openIdConnectScheme}' is already mapped to the Azure Active Directory B2C scheme '{mapping.Key}'");
}
}
o.OpenIDMappings.Add(scheme, new AzureADB2CSchemeOptions.AzureADB2COpenIDSchemeMapping
{
OpenIdConnectScheme = openIdConnectScheme,
CookieScheme = cookieScheme
});
};
}
private static void AddAdditionalMvcApplicationParts(IServiceCollection services)
{
var additionalParts = GetAdditionalParts();
var mvcBuilder = services
.AddMvc()
.ConfigureApplicationPartManager(apm =>
{
foreach (var part in additionalParts)
{
if (!apm.ApplicationParts.Any(ap => HasSameName(ap.Name, part.Name)))
{
apm.ApplicationParts.Add(part);
}
}
apm.FeatureProviders.Add(new AzureADB2CAccountControllerFeatureProvider());
});
bool HasSameName(string left, string right) => string.Equals(left, right, StringComparison.Ordinal);
}
private static IEnumerable<ApplicationPart> GetAdditionalParts()
{
var thisAssembly = typeof(AzureADB2CAuthenticationBuilderExtensions).Assembly;
var relatedAssemblies = RelatedAssemblyAttribute.GetRelatedAssemblies(thisAssembly, throwOnError: true);
foreach (var reference in relatedAssemblies)
{
yield return new CompiledRazorAssemblyPart(reference);
}
}
}
}