Skip to content

Commit b034a7d

Browse files
Add dotnet user-jwts tool and runtime support (#41520)
* Add dotnet dev-jwts tool * Add dotnet dev-jwts tool * Address feedback from review * Rename project file * Write auth config to app settings * Address more feedback * 🦭 * Apply suggestions from code review Co-authored-by: Brennan <[email protected]> * Address more feedback * Add framework support for authentication changes * Add tests for user-jwts CLI and react to feedback * Move ConsoleTable implementation to avoid conflicts in ProjectTemplates * Update existing auth tests and fix middleware registration * Update AzureAdB2C tests and auth app builder * Fix build and move registration check * Fix up resolution for Certificate test sources * Fix write stream configuration for writing key material * Fix handling missing config section when processing options Co-authored-by: Brennan <[email protected]>
1 parent 17bd820 commit b034a7d

File tree

49 files changed

+1884
-2
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1884
-2
lines changed

AspNetCore.sln

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1710,6 +1710,14 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.Html.A
17101710
EndProject
17111711
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "RateLimiting", "RateLimiting", "{1D865E78-7A66-4CA9-92EE-2B350E45281F}"
17121712
EndProject
1713+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "dotnet-user-jwts", "src\Tools\dotnet-user-jwts\src\dotnet-user-jwts.csproj", "{B34CB502-0286-4939-B25F-45998528A802}"
1714+
EndProject
1715+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "dotnet-user-jwts", "dotnet-user-jwts", "{AB4B9E75-719C-4589-B852-20FBFD727730}"
1716+
EndProject
1717+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MinimalJwtBearerSample", "src\Security\Authentication\JwtBearer\samples\MinimalJwtBearerSample\MinimalJwtBearerSample.csproj", "{7F079E92-32D5-4257-B95B-CFFB0D49C160}"
1718+
EndProject
1719+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "dotnet-user-jwts.Tests", "src\Tools\dotnet-user-jwts\test\dotnet-user-jwts.Tests.csproj", "{89896261-C5DD-4901-BCA7-7A5F718BC008}"
1720+
EndProject
17131721
Global
17141722
GlobalSection(SolutionConfigurationPlatforms) = preSolution
17151723
Debug|Any CPU = Debug|Any CPU
@@ -10247,6 +10255,54 @@ Global
1024710255
{487EF7BE-5009-4C70-B79E-45519BDD9603}.Release|x64.Build.0 = Release|Any CPU
1024810256
{487EF7BE-5009-4C70-B79E-45519BDD9603}.Release|x86.ActiveCfg = Release|Any CPU
1024910257
{487EF7BE-5009-4C70-B79E-45519BDD9603}.Release|x86.Build.0 = Release|Any CPU
10258+
{B34CB502-0286-4939-B25F-45998528A802}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
10259+
{B34CB502-0286-4939-B25F-45998528A802}.Debug|Any CPU.Build.0 = Debug|Any CPU
10260+
{B34CB502-0286-4939-B25F-45998528A802}.Debug|arm64.ActiveCfg = Debug|Any CPU
10261+
{B34CB502-0286-4939-B25F-45998528A802}.Debug|arm64.Build.0 = Debug|Any CPU
10262+
{B34CB502-0286-4939-B25F-45998528A802}.Debug|x64.ActiveCfg = Debug|Any CPU
10263+
{B34CB502-0286-4939-B25F-45998528A802}.Debug|x64.Build.0 = Debug|Any CPU
10264+
{B34CB502-0286-4939-B25F-45998528A802}.Debug|x86.ActiveCfg = Debug|Any CPU
10265+
{B34CB502-0286-4939-B25F-45998528A802}.Debug|x86.Build.0 = Debug|Any CPU
10266+
{B34CB502-0286-4939-B25F-45998528A802}.Release|Any CPU.ActiveCfg = Release|Any CPU
10267+
{B34CB502-0286-4939-B25F-45998528A802}.Release|Any CPU.Build.0 = Release|Any CPU
10268+
{B34CB502-0286-4939-B25F-45998528A802}.Release|arm64.ActiveCfg = Release|Any CPU
10269+
{B34CB502-0286-4939-B25F-45998528A802}.Release|arm64.Build.0 = Release|Any CPU
10270+
{B34CB502-0286-4939-B25F-45998528A802}.Release|x64.ActiveCfg = Release|Any CPU
10271+
{B34CB502-0286-4939-B25F-45998528A802}.Release|x64.Build.0 = Release|Any CPU
10272+
{B34CB502-0286-4939-B25F-45998528A802}.Release|x86.ActiveCfg = Release|Any CPU
10273+
{B34CB502-0286-4939-B25F-45998528A802}.Release|x86.Build.0 = Release|Any CPU
10274+
{7F079E92-32D5-4257-B95B-CFFB0D49C160}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
10275+
{7F079E92-32D5-4257-B95B-CFFB0D49C160}.Debug|Any CPU.Build.0 = Debug|Any CPU
10276+
{7F079E92-32D5-4257-B95B-CFFB0D49C160}.Debug|arm64.ActiveCfg = Debug|Any CPU
10277+
{7F079E92-32D5-4257-B95B-CFFB0D49C160}.Debug|arm64.Build.0 = Debug|Any CPU
10278+
{7F079E92-32D5-4257-B95B-CFFB0D49C160}.Debug|x64.ActiveCfg = Debug|Any CPU
10279+
{7F079E92-32D5-4257-B95B-CFFB0D49C160}.Debug|x64.Build.0 = Debug|Any CPU
10280+
{7F079E92-32D5-4257-B95B-CFFB0D49C160}.Debug|x86.ActiveCfg = Debug|Any CPU
10281+
{7F079E92-32D5-4257-B95B-CFFB0D49C160}.Debug|x86.Build.0 = Debug|Any CPU
10282+
{7F079E92-32D5-4257-B95B-CFFB0D49C160}.Release|Any CPU.ActiveCfg = Release|Any CPU
10283+
{7F079E92-32D5-4257-B95B-CFFB0D49C160}.Release|Any CPU.Build.0 = Release|Any CPU
10284+
{7F079E92-32D5-4257-B95B-CFFB0D49C160}.Release|arm64.ActiveCfg = Release|Any CPU
10285+
{7F079E92-32D5-4257-B95B-CFFB0D49C160}.Release|arm64.Build.0 = Release|Any CPU
10286+
{7F079E92-32D5-4257-B95B-CFFB0D49C160}.Release|x64.ActiveCfg = Release|Any CPU
10287+
{7F079E92-32D5-4257-B95B-CFFB0D49C160}.Release|x64.Build.0 = Release|Any CPU
10288+
{7F079E92-32D5-4257-B95B-CFFB0D49C160}.Release|x86.ActiveCfg = Release|Any CPU
10289+
{7F079E92-32D5-4257-B95B-CFFB0D49C160}.Release|x86.Build.0 = Release|Any CPU
10290+
{89896261-C5DD-4901-BCA7-7A5F718BC008}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
10291+
{89896261-C5DD-4901-BCA7-7A5F718BC008}.Debug|Any CPU.Build.0 = Debug|Any CPU
10292+
{89896261-C5DD-4901-BCA7-7A5F718BC008}.Debug|arm64.ActiveCfg = Debug|Any CPU
10293+
{89896261-C5DD-4901-BCA7-7A5F718BC008}.Debug|arm64.Build.0 = Debug|Any CPU
10294+
{89896261-C5DD-4901-BCA7-7A5F718BC008}.Debug|x64.ActiveCfg = Debug|Any CPU
10295+
{89896261-C5DD-4901-BCA7-7A5F718BC008}.Debug|x64.Build.0 = Debug|Any CPU
10296+
{89896261-C5DD-4901-BCA7-7A5F718BC008}.Debug|x86.ActiveCfg = Debug|Any CPU
10297+
{89896261-C5DD-4901-BCA7-7A5F718BC008}.Debug|x86.Build.0 = Debug|Any CPU
10298+
{89896261-C5DD-4901-BCA7-7A5F718BC008}.Release|Any CPU.ActiveCfg = Release|Any CPU
10299+
{89896261-C5DD-4901-BCA7-7A5F718BC008}.Release|Any CPU.Build.0 = Release|Any CPU
10300+
{89896261-C5DD-4901-BCA7-7A5F718BC008}.Release|arm64.ActiveCfg = Release|Any CPU
10301+
{89896261-C5DD-4901-BCA7-7A5F718BC008}.Release|arm64.Build.0 = Release|Any CPU
10302+
{89896261-C5DD-4901-BCA7-7A5F718BC008}.Release|x64.ActiveCfg = Release|Any CPU
10303+
{89896261-C5DD-4901-BCA7-7A5F718BC008}.Release|x64.Build.0 = Release|Any CPU
10304+
{89896261-C5DD-4901-BCA7-7A5F718BC008}.Release|x86.ActiveCfg = Release|Any CPU
10305+
{89896261-C5DD-4901-BCA7-7A5F718BC008}.Release|x86.Build.0 = Release|Any CPU
1025010306
EndGlobalSection
1025110307
GlobalSection(SolutionProperties) = preSolution
1025210308
HideSolutionNode = FALSE
@@ -11094,6 +11150,10 @@ Global
1109411150
{51D07AA9-6297-4F66-A7BD-71CE7E3F4A3F} = {0F84F170-57D0-496B-8E2C-7984178EF69F}
1109511151
{487EF7BE-5009-4C70-B79E-45519BDD9603} = {412D4C15-F48F-4DB1-940A-131D1AA87088}
1109611152
{1D865E78-7A66-4CA9-92EE-2B350E45281F} = {E5963C9F-20A6-4385-B364-814D2581FADF}
11153+
{B34CB502-0286-4939-B25F-45998528A802} = {AB4B9E75-719C-4589-B852-20FBFD727730}
11154+
{AB4B9E75-719C-4589-B852-20FBFD727730} = {0B200A66-B809-4ED3-A790-CB1C2E80975E}
11155+
{7F079E92-32D5-4257-B95B-CFFB0D49C160} = {7FD32066-C831-4E29-978C-9A2215E85C67}
11156+
{89896261-C5DD-4901-BCA7-7A5F718BC008} = {AB4B9E75-719C-4589-B852-20FBFD727730}
1109711157
EndGlobalSection
1109811158
GlobalSection(ExtensibilityGlobals) = postSolution
1109911159
SolutionGuid = {3E8720B3-DBDD-498C-B383-2CC32A054E8F}

eng/Signing.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
<FileSignInfo Include="dotnet-user-secrets.exe" CertificateName="MicrosoftDotNet500" />
8080
<FileSignInfo Include="dotnet-watch.exe" CertificateName="MicrosoftDotNet500" />
8181
<FileSignInfo Include="dotnet-openapi.exe" CertificateName="MicrosoftDotNet500" />
82+
<FileSignInfo Include="dotnet-user-jwts.exe" CertificateName="MicrosoftDotNet500" />
8283
<FileSignInfo Include="Microsoft.AspNetCore.Blazor.Build.exe" CertificateName="MicrosoftDotNet500" />
8384
<FileSignInfo Include="sni.dll" CertificateName="MicrosoftDotNet500" />
8485

src/Azure/AzureAD/Authentication.AzureAD.UI/test/AzureADAuthenticationBuilderExtensionsTests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Microsoft.AspNetCore.Authentication.JwtBearer;
77
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
88
using Microsoft.AspNetCore.Http;
9+
using Microsoft.Extensions.Configuration;
910
using Microsoft.Extensions.DependencyInjection;
1011
using Microsoft.Extensions.Logging;
1112
using Microsoft.Extensions.Logging.Abstractions;
@@ -21,6 +22,7 @@ public void AddAzureAD_AddsAllAuthenticationHandlers()
2122
// Arrange
2223
var services = new ServiceCollection();
2324
services.AddSingleton<ILoggerFactory>(new NullLoggerFactory());
25+
services.AddSingleton<IConfiguration>(new ConfigurationManager());
2426

2527
// Act
2628
services.AddAuthentication()
@@ -288,6 +290,7 @@ public void AddAzureADBearer_AddsAllAuthenticationHandlers()
288290
// Arrange
289291
var services = new ServiceCollection();
290292
services.AddSingleton<ILoggerFactory>(new NullLoggerFactory());
293+
services.AddSingleton<IConfiguration>(new ConfigurationManager());
291294

292295
// Act
293296
services.AddAuthentication()
@@ -305,6 +308,7 @@ public void AddAzureADBearer_ConfiguresAllOptions()
305308
// Arrange
306309
var services = new ServiceCollection();
307310
services.AddSingleton<ILoggerFactory>(new NullLoggerFactory());
311+
services.AddSingleton<IConfiguration>(new ConfigurationManager());
308312

309313
// Act
310314
services.AddAuthentication()
@@ -340,6 +344,7 @@ public void AddAzureADBearer_CanOverrideJwtBearerOptionsConfiguration()
340344
// Arrange
341345
var services = new ServiceCollection();
342346
services.AddSingleton<ILoggerFactory>(new NullLoggerFactory());
347+
services.AddSingleton<IConfiguration>(new ConfigurationManager());
343348

344349
// Act
345350
services.AddAuthentication()
@@ -373,6 +378,7 @@ public void AddAzureADBearer_RegisteringJwtBearerHasNoImpactOnAzureAAExtensions(
373378
// Arrange
374379
var services = new ServiceCollection();
375380
services.AddSingleton<ILoggerFactory>(new NullLoggerFactory());
381+
services.AddSingleton<IConfiguration>(new ConfigurationManager());
376382

377383
// Act
378384
services.AddAuthentication()
@@ -473,6 +479,7 @@ public void AddAzureADBearer_SkipsOptionsValidationForNonAzureCookies()
473479
{
474480
var services = new ServiceCollection();
475481
services.AddSingleton<ILoggerFactory>(new NullLoggerFactory());
482+
services.AddSingleton<IConfiguration>(new ConfigurationManager());
476483

477484
services.AddAuthentication()
478485
.AddAzureADBearer(o => { })

src/Azure/AzureAD/Authentication.AzureADB2C.UI/test/AzureAdB2CAuthenticationBuilderExtensionsTests.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Microsoft.AspNetCore.Authentication.JwtBearer;
77
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
88
using Microsoft.AspNetCore.Http;
9+
using Microsoft.Extensions.Configuration;
910
using Microsoft.Extensions.DependencyInjection;
1011
using Microsoft.Extensions.Logging;
1112
using Microsoft.Extensions.Logging.Abstractions;
@@ -262,6 +263,7 @@ public void AddAzureADB2CBearer_AddsAllAuthenticationHandlers()
262263
// Arrange
263264
var services = new ServiceCollection();
264265
services.AddSingleton<ILoggerFactory>(new NullLoggerFactory());
266+
services.AddSingleton<IConfiguration>(new ConfigurationManager());
265267

266268
// Act
267269
services.AddAuthentication()
@@ -279,6 +281,7 @@ public void AddAzureADB2CBearer_ConfiguresAllOptions()
279281
// Arrange
280282
var services = new ServiceCollection();
281283
services.AddSingleton<ILoggerFactory>(new NullLoggerFactory());
284+
services.AddSingleton<IConfiguration>(new ConfigurationManager());
282285

283286
// Act
284287
services.AddAuthentication()
@@ -315,6 +318,7 @@ public void AddAzureADB2CBearer_CanOverrideJwtBearerOptionsConfiguration()
315318
// Arrange
316319
var services = new ServiceCollection();
317320
services.AddSingleton<ILoggerFactory>(new NullLoggerFactory());
321+
services.AddSingleton<IConfiguration>(new ConfigurationManager());
318322

319323
// Act
320324
services.AddAuthentication()
@@ -348,6 +352,7 @@ public void AddAzureADB2CBearer_RegisteringJwtBearerHasNoImpactOnAzureAAExtensio
348352
// Arrange
349353
var services = new ServiceCollection();
350354
services.AddSingleton<ILoggerFactory>(new NullLoggerFactory());
355+
services.AddSingleton<IConfiguration>(new ConfigurationManager());
351356

352357
// Act
353358
services.AddAuthentication()

src/DefaultBuilder/src/Microsoft.AspNetCore.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
</PropertyGroup>
1212

1313
<ItemGroup>
14+
<Reference Include="Microsoft.AspNetCore.Authentication" />
15+
<Reference Include="Microsoft.AspNetCore.Authorization.Policy" />
1416
<Reference Include="Microsoft.AspNetCore.Diagnostics" />
1517
<Reference Include="Microsoft.AspNetCore.HostFiltering" />
1618
<Reference Include="Microsoft.AspNetCore.Hosting" />
@@ -32,4 +34,8 @@
3234
<Reference Include="Microsoft.Extensions.Logging.EventSource" />
3335
</ItemGroup>
3436

37+
<ItemGroup>
38+
<InternalsVisibleTo Include="Microsoft.AspNetCore.Authentication.Test" />
39+
</ItemGroup>
40+
3541
</Project>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
#nullable enable
22
Microsoft.AspNetCore.Builder.WebApplication.Use(System.Func<Microsoft.AspNetCore.Http.RequestDelegate!, Microsoft.AspNetCore.Http.RequestDelegate!>! middleware) -> Microsoft.AspNetCore.Builder.IApplicationBuilder!
3+
Microsoft.AspNetCore.Builder.WebApplicationBuilder.Authentication.get -> Microsoft.AspNetCore.Authentication.AuthenticationBuilder!
34
static Microsoft.Extensions.Hosting.GenericHostBuilderExtensions.ConfigureWebHostDefaults(this Microsoft.Extensions.Hosting.IHostBuilder! builder, System.Action<Microsoft.AspNetCore.Hosting.IWebHostBuilder!>! configure, System.Action<Microsoft.Extensions.Hosting.WebHostBuilderOptions!>! configureOptions) -> Microsoft.Extensions.Hosting.IHostBuilder!
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Diagnostics.CodeAnalysis;
5+
using Microsoft.Extensions.DependencyInjection;
6+
7+
namespace Microsoft.AspNetCore.Authentication;
8+
9+
internal class WebApplicationAuthenticationBuilder : AuthenticationBuilder
10+
{
11+
public bool IsAuthenticationConfigured { get; private set; }
12+
13+
public WebApplicationAuthenticationBuilder(IServiceCollection services) : base(services) { }
14+
15+
public override AuthenticationBuilder AddPolicyScheme(string authenticationScheme, string? displayName, Action<PolicySchemeOptions> configureOptions)
16+
{
17+
RegisterServices(authenticationScheme);
18+
return base.AddPolicyScheme(authenticationScheme, displayName, configureOptions);
19+
}
20+
21+
public override AuthenticationBuilder AddRemoteScheme<TOptions, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] THandler>(string authenticationScheme, string? displayName, Action<TOptions>? configureOptions)
22+
{
23+
RegisterServices(authenticationScheme);
24+
return base.AddRemoteScheme<TOptions, THandler>(authenticationScheme, displayName, configureOptions);
25+
}
26+
27+
public override AuthenticationBuilder AddScheme<TOptions, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] THandler>(string authenticationScheme, string? displayName, Action<TOptions>? configureOptions)
28+
{
29+
RegisterServices(authenticationScheme);
30+
return base.AddScheme<TOptions, THandler>(authenticationScheme, displayName, configureOptions);
31+
}
32+
33+
public override AuthenticationBuilder AddScheme<TOptions, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] THandler>(string authenticationScheme, Action<TOptions>? configureOptions)
34+
{
35+
RegisterServices(authenticationScheme);
36+
return base.AddScheme<TOptions, THandler>(authenticationScheme, configureOptions);
37+
}
38+
39+
private void RegisterServices(string authenticationScheme)
40+
{
41+
if (!IsAuthenticationConfigured)
42+
{
43+
IsAuthenticationConfigured = true;
44+
Services.AddAuthentication(authenticationScheme);
45+
Services.AddAuthorization();
46+
}
47+
}
48+
}

src/DefaultBuilder/src/WebApplicationBuilder.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Diagnostics;
5+
using Microsoft.AspNetCore.Authentication;
56
using Microsoft.AspNetCore.Hosting;
67
using Microsoft.Extensions.Configuration;
78
using Microsoft.Extensions.DependencyInjection;
@@ -16,9 +17,11 @@ namespace Microsoft.AspNetCore.Builder;
1617
public sealed class WebApplicationBuilder
1718
{
1819
private const string EndpointRouteBuilderKey = "__EndpointRouteBuilder";
20+
private const string AuthenticationMiddlewareSetKey = "__AuthenticationMiddlewareSet";
1921

2022
private readonly HostApplicationBuilder _hostApplicationBuilder;
2123
private readonly ServiceDescriptor _genericWebHostServiceDescriptor;
24+
private readonly WebApplicationAuthenticationBuilder _webAuthBuilder;
2225

2326
private WebApplication? _builtApplication;
2427

@@ -79,6 +82,7 @@ internal WebApplicationBuilder(WebApplicationOptions options, Action<IHostBuilde
7982

8083
Host = new ConfigureHostBuilder(bootstrapHostBuilder.Context, Configuration, Services);
8184
WebHost = new ConfigureWebHostBuilder(webHostContext, Configuration, Services);
85+
_webAuthBuilder = new WebApplicationAuthenticationBuilder(Services);
8286
}
8387

8488
/// <summary>
@@ -113,6 +117,11 @@ internal WebApplicationBuilder(WebApplicationOptions options, Action<IHostBuilde
113117
/// </summary>
114118
public ConfigureHostBuilder Host { get; }
115119

120+
/// <summary>
121+
/// An <see cref="AuthenticationBuilder"/> for configuration authentication-related properties.
122+
/// </summary>
123+
public AuthenticationBuilder Authentication => _webAuthBuilder;
124+
116125
/// <summary>
117126
/// Builds the <see cref="WebApplication"/>.
118127
/// </summary>
@@ -166,6 +175,16 @@ private void ConfigureApplication(WebHostBuilderContext context, IApplicationBui
166175
}
167176
}
168177

178+
if (_webAuthBuilder.IsAuthenticationConfigured)
179+
{
180+
// Don't add more than one instance of the middleware
181+
if (!_builtApplication.Properties.ContainsKey(AuthenticationMiddlewareSetKey))
182+
{
183+
_builtApplication.UseAuthentication();
184+
_builtApplication.UseAuthorization();
185+
}
186+
}
187+
169188
// Wire the source pipeline to run in the destination pipeline
170189
app.Use(next =>
171190
{
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using Microsoft.Extensions.Configuration;
5+
6+
namespace Microsoft.AspNetCore.Authentication;
7+
8+
/// <summary>
9+
/// Provides an interface for implmenting a construct that provides
10+
/// access to specific configuration sections.
11+
/// </summary>
12+
public interface IAuthenticationConfigurationProvider
13+
{
14+
/// <summary>
15+
/// Returns the specified <see cref="ConfigurationSection"/> object.
16+
/// </summary>
17+
/// <param name="authenticationScheme">The path to the section to be returned.</param>
18+
/// <returns>The specified <see cref="ConfigurationSection"/> object, or null if the requested section does not exist.</returns>
19+
IConfiguration GetAuthenticationSchemeConfiguration(string authenticationScheme);
20+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
#nullable enable
2+
Microsoft.AspNetCore.Authentication.IAuthenticationConfigurationProvider
3+
Microsoft.AspNetCore.Authentication.IAuthenticationConfigurationProvider.GetAuthenticationSchemeConfiguration(string! authenticationScheme) -> Microsoft.Extensions.Configuration.IConfiguration!

src/Middleware/HttpOverrides/test/CertificateForwardingTest.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Security.Cryptography.X509Certificates;
5+
using Microsoft.AspNetCore.Authentication.Certificate;
56
using Microsoft.AspNetCore.Builder;
67
using Microsoft.AspNetCore.Hosting;
78
using Microsoft.AspNetCore.TestHost;

src/Security/Authentication/Core/src/AuthAppBuilderExtensions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ namespace Microsoft.AspNetCore.Builder;
1010
/// </summary>
1111
public static class AuthAppBuilderExtensions
1212
{
13+
internal const string AuthenticationMiddlewareSetKey = "__AuthenticationMiddlewareSet";
14+
1315
/// <summary>
1416
/// Adds the <see cref="AuthenticationMiddleware"/> to the specified <see cref="IApplicationBuilder"/>, which enables authentication capabilities.
1517
/// </summary>
@@ -22,6 +24,7 @@ public static IApplicationBuilder UseAuthentication(this IApplicationBuilder app
2224
throw new ArgumentNullException(nameof(app));
2325
}
2426

27+
app.Properties[AuthenticationMiddlewareSetKey] = true;
2528
return app.UseMiddleware<AuthenticationMiddleware>();
2629
}
2730
}

src/Security/Authentication/Core/src/AuthenticationServiceCollectionExtensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public static AuthenticationBuilder AddAuthentication(this IServiceCollection se
2727
services.AddDataProtection();
2828
services.AddWebEncoders();
2929
services.TryAddSingleton<ISystemClock, SystemClock>();
30+
services.TryAddSingleton<IAuthenticationConfigurationProvider, DefaultAuthenticationConfigurationProvider>();
3031
return new AuthenticationBuilder(services);
3132
}
3233

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using Microsoft.Extensions.Configuration;
5+
6+
namespace Microsoft.AspNetCore.Authentication;
7+
8+
internal sealed class DefaultAuthenticationConfigurationProvider : IAuthenticationConfigurationProvider
9+
{
10+
private readonly IConfiguration _configuration;
11+
12+
public DefaultAuthenticationConfigurationProvider(IConfiguration configuration)
13+
{
14+
_configuration = configuration;
15+
}
16+
17+
public IConfiguration GetAuthenticationSchemeConfiguration(string authenticationScheme)
18+
{
19+
return _configuration.GetSection($"Authentication:Schemes:{authenticationScheme}");
20+
}
21+
}

0 commit comments

Comments
 (0)