-
Notifications
You must be signed in to change notification settings - Fork 270
Expand file tree
/
Copy pathAppBuilderExtension.cs
More file actions
264 lines (234 loc) · 13.3 KB
/
Copy pathAppBuilderExtension.cs
File metadata and controls
264 lines (234 loc) · 13.3 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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using System.Web;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Microsoft.Identity.Abstractions;
using Microsoft.Identity.Client;
using Microsoft.Identity.Web.Hosts;
using Microsoft.Identity.Web.OWIN;
using Microsoft.IdentityModel.Protocols.OpenIdConnect;
using Microsoft.IdentityModel.Tokens;
using Microsoft.IdentityModel.Validators;
using Microsoft.Owin.Security.Jwt;
using Microsoft.Owin.Security.OAuth;
using Microsoft.Owin.Security.OpenIdConnect;
using Owin;
namespace Microsoft.Identity.Web
{
/// <summary>
/// Extension methods on an ASP.NET application to add a web app or web API.
/// </summary>
public static class AppBuilderExtension
{
static internal TokenAcquirerFactory? TokenAcquirerFactory { get; set; }
/// <summary>
/// Configuration.
/// </summary>
static internal IConfiguration? Configuration { get { return TokenAcquirerFactory?.Configuration; } }
/// <summary>
/// Service provider.
/// </summary>
static internal IServiceProvider? ServiceProvider { get { return TokenAcquirerFactory?.ServiceProvider; } }
/// <summary>
/// Adds a protected web API.
/// </summary>
/// <param name="app">Application builder.</param>
/// <param name="configureServices">Configure the services (if you want to call downstream web APIs).</param>
/// <param name="configureMicrosoftAuthenticationOptions">Configure Microsoft authentication options.</param>
/// <param name="updateOptions">Update the OWIN options if you want to finesse the token validation.</param>
/// <param name="configurationSection">Configuration section in which to read the options.</param>
/// <returns>The app builder to chain.</returns>
public static IAppBuilder AddMicrosoftIdentityWebApi(
this IAppBuilder app,
Action<IServiceCollection>? configureServices = null,
Action<MicrosoftAuthenticationOptions>? configureMicrosoftAuthenticationOptions = null,
Action<OAuthBearerAuthenticationOptions>? updateOptions = null,
string configurationSection = "AzureAd")
{
TokenAcquirerFactory = TokenAcquirerFactory.GetDefaultInstance<OwinTokenAcquirerFactory>();
var services = TokenAcquirerFactory.Services;
// Configure the Microsoft authentication options
services.Configure(
string.Empty,
configureMicrosoftAuthenticationOptions ?? (option =>
{
Configuration?.GetSection(configurationSection).Bind(option);
}));
services.Configure<ConfidentialClientApplicationOptions>(
string.Empty,
(option =>
{
Configuration?.GetSection(configurationSection).Bind(option);
}));
configureServices?.Invoke(services);
// Replace the genenric host by an OWIN host
ServiceDescriptor? tokenAcquisitionhost = services.FirstOrDefault(s => s.ServiceType == typeof(ITokenAcquisitionHost));
if (tokenAcquisitionhost != null)
{
services.Remove(tokenAcquisitionhost);
if (tokenAcquisitionhost.Lifetime == ServiceLifetime.Singleton)
{
// The service was already added, but not with the right lifetime
services.AddSingleton<ITokenAcquisitionHost, OwinTokenAcquisitionHost>();
}
else
{
// The service is already added with the right lifetime
services.AddScoped<ITokenAcquisitionHost, OwinTokenAcquisitionHost>();
}
}
TokenAcquirerFactory.Build();
string instance = Configuration.GetValue<string>($"{configurationSection}:Instance");
string tenantId = Configuration.GetValue<string>($"{configurationSection}:TenantId");
string clientId = Configuration.GetValue<string>($"{configurationSection}:ClientId");
string audience = Configuration.GetValue<string>($"{configurationSection}:Audience");
string authority = instance + tenantId + "/v2.0";
TokenValidationParameters tokenValidationParameters = new TokenValidationParameters
{
ValidAudience = audience ?? clientId,
IssuerValidator = AadIssuerValidator.GetAadIssuerValidator(authority).Validate,
SaveSigninToken = true,
};
OAuthBearerAuthenticationOptions options = new OAuthBearerAuthenticationOptions
{
AccessTokenFormat = new JwtFormat(tokenValidationParameters, new OpenIdConnectCachingSecurityTokenProvider(authority + "/.well-known/openid-configuration"))
};
if (updateOptions != null)
{
updateOptions(options);
}
return app.UseOAuthBearerAuthentication(options);
}
/// <summary>
/// Adds a protected web app.
/// </summary>
/// <param name="app">Application builder.</param>
/// <param name="configureServices">Configure the services (if you want to call downstream web APIs).</param>
/// <param name="configureMicrosoftAuthenticationOptions">Configure Microsoft authentication options.</param>
/// <param name="updateOptions">Update the OWIN options if you want to finesse the OpenIdConnect options.</param>
/// <param name="configurationSection">Configuration section in which to read the options.</param>
/// <returns>The app builder to chain.</returns>
public static IAppBuilder AddMicrosoftIdentityWebApp(
this IAppBuilder app,
Action<IServiceCollection>? configureServices = null,
Action<MicrosoftAuthenticationOptions>? configureMicrosoftAuthenticationOptions = null,
Action<OpenIdConnectAuthenticationOptions>? updateOptions = null,
string configurationSection = "AzureAd")
{
TokenAcquirerFactory = TokenAcquirerFactory.GetDefaultInstance<OwinTokenAcquirerFactory>();
var services = TokenAcquirerFactory.Services;
// Configure the Microsoft authentication options
services.Configure(
string.Empty,
configureMicrosoftAuthenticationOptions ?? (option =>
{
Configuration?.GetSection(configurationSection).Bind(option);
}));
services.Configure<ConfidentialClientApplicationOptions>(
string.Empty,
(option =>
{
Configuration?.GetSection(configurationSection).Bind(option);
}));
configureServices?.Invoke(services);
// Replace the genenric host by an OWIN host
ServiceDescriptor? tokenAcquisitionhost = services.FirstOrDefault(s => s.ServiceType == typeof(ITokenAcquisitionHost));
if (tokenAcquisitionhost != null)
{
services.Remove(tokenAcquisitionhost);
if (tokenAcquisitionhost.Lifetime == ServiceLifetime.Singleton)
{
// The service was already added, but not with the right lifetime
services.AddSingleton<ITokenAcquisitionHost, OwinTokenAcquisitionHost>();
}
else
{
// The service is already added with the right lifetime
services.AddScoped<ITokenAcquisitionHost, OwinTokenAcquisitionHost>();
}
}
TokenAcquirerFactory.Build();
string instance = Configuration.GetValue<string>($"{configurationSection}:Instance");
string tenantId = Configuration.GetValue<string>($"{configurationSection}:TenantId");
string clientId = Configuration.GetValue<string>($"{configurationSection}:ClientId");
string postLogoutRedirectUri = Configuration.GetValue<string>($"{configurationSection}:SignedOutCallbackPath");
string authority = instance + tenantId + "/v2.0";
OpenIdConnectAuthenticationOptions options = new OpenIdConnectAuthenticationOptions
{
ClientId = clientId,
Authority = authority,
PostLogoutRedirectUri = postLogoutRedirectUri,
Scope = "openid profile offline_access user.read",
ResponseType = "code",
Notifications = new OpenIdConnectAuthenticationNotifications()
{
RedirectToIdentityProvider = (context) =>
{
var loginHint = context.ProtocolMessage.GetParameter(OpenIdConnectParameterNames.LoginHint);
if (!string.IsNullOrWhiteSpace(loginHint))
{
context.ProtocolMessage.LoginHint = loginHint;
context.ProtocolMessage.SetParameter(Constants.XAnchorMailbox, $"{Constants.Upn}:{loginHint}");
// delete the login_hint from the Properties when we are done otherwise
// it will take up extra space in the cookie.
context.ProtocolMessage.Parameters.Remove(OpenIdConnectParameterNames.LoginHint);
}
var domainHint = context.ProtocolMessage.GetParameter(OpenIdConnectParameterNames.DomainHint);
if (!string.IsNullOrWhiteSpace(domainHint))
{
context.ProtocolMessage.DomainHint = domainHint;
// delete the domain_hint from the Properties when we are done otherwise
// it will take up extra space in the cookie.
context.ProtocolMessage.Parameters.Remove(OpenIdConnectParameterNames.DomainHint);
}
context.ProtocolMessage.SetParameter(ClaimConstants.ClientInfo, Constants.One);
context.ProtocolMessage.SetParameter(Constants.TelemetryHeaderKey, IdHelper.CreateTelemetryInfo());
return Task.CompletedTask;
},
SecurityTokenValidated = (context) =>
{
HttpContextBase httpContext = context.OwinContext.Get<HttpContextBase>(typeof(HttpContextBase).FullName);
string? clientInfo = httpContext.Session[ClaimConstants.ClientInfo] as string;
if (clientInfo!=null && !string.IsNullOrEmpty(clientInfo))
{
ClientInfo? clientInfoFromServer = ClientInfo.CreateFromJson(clientInfo);
if (clientInfoFromServer != null && clientInfoFromServer.UniqueTenantIdentifier != null && clientInfoFromServer.UniqueObjectIdentifier != null)
{
context.AuthenticationTicket.Identity.AddClaim(new Claim(ClaimConstants.UniqueTenantIdentifier, clientInfoFromServer.UniqueTenantIdentifier));
context.AuthenticationTicket.Identity.AddClaim(new Claim(ClaimConstants.UniqueObjectIdentifier, clientInfoFromServer.UniqueObjectIdentifier));
}
httpContext.Session.Remove(ClaimConstants.ClientInfo);
}
string name = context.AuthenticationTicket.Identity.FindFirst("preferred_username").Value;
context.AuthenticationTicket.Identity.AddClaim(new Claim(ClaimTypes.Name, name, string.Empty));
return Task.CompletedTask;
},
}
};
options.Notifications.AuthorizationCodeReceived = async context =>
{
context.TokenEndpointRequest.Parameters.TryGetValue("code_verifier", out string codeVerifier);
var tokenAcquisition = TokenAcquirerFactory?.ServiceProvider?.GetRequiredService<ITokenAcquisitionInternal>();
var msIdentityOptions = TokenAcquirerFactory?.ServiceProvider?.GetRequiredService<IOptions<MicrosoftIdentityOptions>>();
var idToken = await (tokenAcquisition!.AddAccountToCacheFromAuthorizationCodeAsync(
new string[] { options.Scope },
context.Code,
string.Empty,
context.ProtocolMessage.GetParameter(ClaimConstants.ClientInfo),
codeVerifier,
msIdentityOptions?.Value.DefaultUserFlow)).ConfigureAwait(false);
HttpContextBase httpContext = context.OwinContext.Get<HttpContextBase>(typeof(HttpContextBase).FullName);
httpContext.Session.Add(ClaimConstants.ClientInfo, context.ProtocolMessage.GetParameter(ClaimConstants.ClientInfo));
context.HandleCodeRedemption(null, idToken);
};
updateOptions?.Invoke(options);
return app.UseOpenIdConnectAuthentication(options);
}
}
}