From 12ee3326ff5029337731a22bfefa8c579614d70f Mon Sep 17 00:00:00 2001 From: Safia Abdalla Date: Tue, 5 Jul 2022 21:48:26 +0000 Subject: [PATCH 1/5] Remove Authentication property from WebApplicationBuilder --- .../src/PublicAPI.Unshipped.txt | 1 - .../WebApplicationAuthenticationBuilder.cs | 48 ------------------- .../src/WebApplicationBuilder.cs | 22 --------- .../WebApplicationTests.cs | 45 ----------------- .../samples/MinimalJwtBearerSample/Program.cs | 10 ++-- .../test/AuthenticationMiddlewareTests.cs | 8 ++-- 6 files changed, 7 insertions(+), 127 deletions(-) delete mode 100644 src/DefaultBuilder/src/WebApplicationAuthenticationBuilder.cs diff --git a/src/DefaultBuilder/src/PublicAPI.Unshipped.txt b/src/DefaultBuilder/src/PublicAPI.Unshipped.txt index 6eba7bee0ff8..88ff0b5ecc80 100644 --- a/src/DefaultBuilder/src/PublicAPI.Unshipped.txt +++ b/src/DefaultBuilder/src/PublicAPI.Unshipped.txt @@ -1,4 +1,3 @@ #nullable enable Microsoft.AspNetCore.Builder.WebApplication.Use(System.Func! middleware) -> Microsoft.AspNetCore.Builder.IApplicationBuilder! -Microsoft.AspNetCore.Builder.WebApplicationBuilder.Authentication.get -> Microsoft.AspNetCore.Authentication.AuthenticationBuilder! static Microsoft.Extensions.Hosting.GenericHostBuilderExtensions.ConfigureWebHostDefaults(this Microsoft.Extensions.Hosting.IHostBuilder! builder, System.Action! configure, System.Action! configureOptions) -> Microsoft.Extensions.Hosting.IHostBuilder! diff --git a/src/DefaultBuilder/src/WebApplicationAuthenticationBuilder.cs b/src/DefaultBuilder/src/WebApplicationAuthenticationBuilder.cs deleted file mode 100644 index f53de4ea8f25..000000000000 --- a/src/DefaultBuilder/src/WebApplicationAuthenticationBuilder.cs +++ /dev/null @@ -1,48 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -using System.Diagnostics.CodeAnalysis; -using Microsoft.Extensions.DependencyInjection; - -namespace Microsoft.AspNetCore.Authentication; - -internal class WebApplicationAuthenticationBuilder : AuthenticationBuilder -{ - public bool IsAuthenticationConfigured { get; private set; } - - public WebApplicationAuthenticationBuilder(IServiceCollection services) : base(services) { } - - public override AuthenticationBuilder AddPolicyScheme(string authenticationScheme, string? displayName, Action configureOptions) - { - RegisterServices(); - return base.AddPolicyScheme(authenticationScheme, displayName, configureOptions); - } - - public override AuthenticationBuilder AddRemoteScheme(string authenticationScheme, string? displayName, Action? configureOptions) - { - RegisterServices(); - return base.AddRemoteScheme(authenticationScheme, displayName, configureOptions); - } - - public override AuthenticationBuilder AddScheme(string authenticationScheme, string? displayName, Action? configureOptions) - { - RegisterServices(); - return base.AddScheme(authenticationScheme, displayName, configureOptions); - } - - public override AuthenticationBuilder AddScheme(string authenticationScheme, Action? configureOptions) - { - RegisterServices(); - return base.AddScheme(authenticationScheme, configureOptions); - } - - private void RegisterServices() - { - if (!IsAuthenticationConfigured) - { - IsAuthenticationConfigured = true; - Services.AddAuthentication(); - Services.AddAuthorization(); - } - } -} diff --git a/src/DefaultBuilder/src/WebApplicationBuilder.cs b/src/DefaultBuilder/src/WebApplicationBuilder.cs index d081da2532a6..c5eabba6816d 100644 --- a/src/DefaultBuilder/src/WebApplicationBuilder.cs +++ b/src/DefaultBuilder/src/WebApplicationBuilder.cs @@ -2,7 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Diagnostics; -using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; @@ -17,11 +16,9 @@ namespace Microsoft.AspNetCore.Builder; public sealed class WebApplicationBuilder { private const string EndpointRouteBuilderKey = "__EndpointRouteBuilder"; - private const string AuthenticationMiddlewareSetKey = "__AuthenticationMiddlewareSet"; private readonly HostApplicationBuilder _hostApplicationBuilder; private readonly ServiceDescriptor _genericWebHostServiceDescriptor; - private readonly WebApplicationAuthenticationBuilder _webAuthBuilder; private WebApplication? _builtApplication; @@ -82,7 +79,6 @@ internal WebApplicationBuilder(WebApplicationOptions options, Action @@ -117,11 +113,6 @@ internal WebApplicationBuilder(WebApplicationOptions options, Action public ConfigureHostBuilder Host { get; } - /// - /// An for configuring authentication-related properties. - /// - public AuthenticationBuilder Authentication => _webAuthBuilder; - /// /// Builds the . /// @@ -175,19 +166,6 @@ private void ConfigureApplication(WebHostBuilderContext context, IApplicationBui } } - if (_webAuthBuilder.IsAuthenticationConfigured) - { - // Don't add more than one instance of the middleware - if (!_builtApplication.Properties.ContainsKey(AuthenticationMiddlewareSetKey)) - { - // The Use invocations will set the property on the outer pipeline, - // but we want to set it on the inner pipeline as well - _builtApplication.Properties[AuthenticationMiddlewareSetKey] = true; - app.UseAuthentication(); - app.UseAuthorization(); - } - } - // Wire the source pipeline to run in the destination pipeline app.Use(next => { diff --git a/src/DefaultBuilder/test/Microsoft.AspNetCore.Tests/WebApplicationTests.cs b/src/DefaultBuilder/test/Microsoft.AspNetCore.Tests/WebApplicationTests.cs index 5f74a0954d0f..825cea8ea3cc 100644 --- a/src/DefaultBuilder/test/Microsoft.AspNetCore.Tests/WebApplicationTests.cs +++ b/src/DefaultBuilder/test/Microsoft.AspNetCore.Tests/WebApplicationTests.cs @@ -1982,51 +1982,6 @@ public void CanObserveDefaultServicesInServiceCollection() Assert.Contains(builder.Services, service => service.ServiceType == typeof(ILogger<>)); } - [Fact] - public async Task RegisterAuthMiddlewaresCorrectly() - { - var helloEndpointCalled = false; - var customMiddlewareExecuted = false; - var username = "foobar"; - - var builder = WebApplication.CreateBuilder(); - builder.Services.AddAuthenticationCore(o => - { - o.DefaultScheme = "testSchemeName"; - }); - builder.Authentication.AddScheme("testSchemeName", "testDisplayName", _ => { }); - builder.WebHost.UseTestServer(); - await using var app = builder.Build(); - - app.Use(next => - { - return async context => - { - // IAuthenticationFeature is added by the authentication middleware - // during invocation. This middleware should run after authentication - // and be able to access the feature. - var authFeature = context.Features.Get(); - Assert.NotNull(authFeature); - customMiddlewareExecuted = true; - Assert.Equal(username, context.User.Identity.Name); - await next(context); - }; - }); - - app.MapGet("/hello", (ClaimsPrincipal user) => - { - helloEndpointCalled = true; - Assert.Equal(username, user.Identity.Name); - }).AllowAnonymous(); - - await app.StartAsync(); - var client = app.GetTestClient(); - await client.GetStringAsync($"/hello?username={username}"); - - Assert.True(helloEndpointCalled); - Assert.True(customMiddlewareExecuted); - } - private class UberHandler : AuthenticationHandler { public UberHandler(IOptionsMonitor options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock) : base(options, logger, encoder, clock) { } diff --git a/src/Security/Authentication/JwtBearer/samples/MinimalJwtBearerSample/Program.cs b/src/Security/Authentication/JwtBearer/samples/MinimalJwtBearerSample/Program.cs index 470b722d6ef6..734c8e413d05 100644 --- a/src/Security/Authentication/JwtBearer/samples/MinimalJwtBearerSample/Program.cs +++ b/src/Security/Authentication/JwtBearer/samples/MinimalJwtBearerSample/Program.cs @@ -2,15 +2,13 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Security.Claims; -using Microsoft.AspNetCore.Authentication.JwtBearer; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.AspNetCore.Builder; var builder = WebApplication.CreateBuilder(args); -builder.Authentication.AddJwtBearer(); -builder.Authentication.AddJwtBearer("ClaimedDetails"); -builder.Authentication.AddJwtBearer("InvalidScheme"); +builder.Services.AddAuthentication() + .AddJwtBearer() + .AddJwtBearer("ClaimedDetails") + .AddJwtBearer("InvalidScheme"); builder.Services.AddAuthorization(options => options.AddPolicy("is_admin", policy => diff --git a/src/Security/Authentication/test/AuthenticationMiddlewareTests.cs b/src/Security/Authentication/test/AuthenticationMiddlewareTests.cs index 9c11dc1e35c1..458c30f69288 100644 --- a/src/Security/Authentication/test/AuthenticationMiddlewareTests.cs +++ b/src/Security/Authentication/test/AuthenticationMiddlewareTests.cs @@ -162,19 +162,17 @@ public async Task WebApplicationBuilder_RegistersAuthenticationMiddlewares() new KeyValuePair("Authentication:Schemes:Bearer:ClaimsIssuer", "SomeIssuer"), new KeyValuePair("Authentication:Schemes:Bearer:Audiences:0", "https://localhost:5001") }); - builder.Authentication.AddJwtBearer(); + builder.Services.AddAuthentication().AddJwtBearer(); await using var app = builder.Build(); - var webAppAuthBuilder = Assert.IsType(builder.Authentication); - Assert.True(webAppAuthBuilder.IsAuthenticationConfigured); - // Authentication middleware isn't registered until application // is built on startup Assert.False(app.Properties.ContainsKey("__AuthenticationMiddlewareSet")); await app.StartAsync(); - Assert.True(app.Properties.ContainsKey("__AuthenticationMiddlewareSet")); + // TODO: Validate after restructuring auto-registration of middlewares. + // Assert.True(app.Properties.ContainsKey("__AuthenticationMiddlewareSet")); var options = app.Services.GetService>().Get(JwtBearerDefaults.AuthenticationScheme); Assert.Equal(new[] { "SomeIssuer" }, options.TokenValidationParameters.ValidIssuers); From f07500427b57b1506e06955789eafa477513f045 Mon Sep 17 00:00:00 2001 From: Safia Abdalla Date: Wed, 6 Jul 2022 19:31:27 +0000 Subject: [PATCH 2/5] Add back support for auto-registering middlewares --- .../src/WebApplicationBuilder.cs | 27 +++++++ .../WebApplicationTests.cs | 80 +++++++++++++++++++ ...ticationCoreServiceCollectionExtensions.cs | 1 + .../src/AuthenticationMarkerService.cs | 8 ++ ...soft.AspNetCore.Authentication.Core.csproj | 4 + .../test/AuthenticationMiddlewareTests.cs | 24 +++++- .../src/AuthorizationAppBuilderExtensions.cs | 3 + ...oft.AspNetCore.Authorization.Policy.csproj | 4 + 8 files changed, 148 insertions(+), 3 deletions(-) create mode 100644 src/Http/Authentication.Core/src/AuthenticationMarkerService.cs diff --git a/src/DefaultBuilder/src/WebApplicationBuilder.cs b/src/DefaultBuilder/src/WebApplicationBuilder.cs index c5eabba6816d..27b199d216fa 100644 --- a/src/DefaultBuilder/src/WebApplicationBuilder.cs +++ b/src/DefaultBuilder/src/WebApplicationBuilder.cs @@ -2,6 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Diagnostics; +using Microsoft.AspNetCore.Authentication; +using Microsoft.AspNetCore.Authorization.Policy; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; @@ -16,6 +18,8 @@ namespace Microsoft.AspNetCore.Builder; public sealed class WebApplicationBuilder { private const string EndpointRouteBuilderKey = "__EndpointRouteBuilder"; + private const string AuthenticationMiddlewareSetKey = "__AuthenticationMiddlewareSet"; + private const string AuthorizationMiddlewareSetKey = "__AuthorizationMiddlewareSet"; private readonly HostApplicationBuilder _hostApplicationBuilder; private readonly ServiceDescriptor _genericWebHostServiceDescriptor; @@ -166,6 +170,29 @@ private void ConfigureApplication(WebHostBuilderContext context, IApplicationBui } } + // Process authorization and authentication middlewares independently to avoid + // registering middlewares for services that do not exist + if (_builtApplication.Services.GetService() != null) + { + // Don't add more than one instance of the middleware + if (!_builtApplication.Properties.ContainsKey(AuthenticationMiddlewareSetKey)) + { + // The Use invocations will set the property on the outer pipeline, + // but we want to set it on the inner pipeline as well. + _builtApplication.Properties[AuthenticationMiddlewareSetKey] = true; + app.UseAuthentication(); + } + } + + if (_builtApplication.Services.GetService() != null) + { + if (!_builtApplication.Properties.ContainsKey(AuthorizationMiddlewareSetKey)) + { + _builtApplication.Properties[AuthorizationMiddlewareSetKey] = true; + app.UseAuthorization(); + } + } + // Wire the source pipeline to run in the destination pipeline app.Use(next => { diff --git a/src/DefaultBuilder/test/Microsoft.AspNetCore.Tests/WebApplicationTests.cs b/src/DefaultBuilder/test/Microsoft.AspNetCore.Tests/WebApplicationTests.cs index 825cea8ea3cc..fd4bda6dcaa2 100644 --- a/src/DefaultBuilder/test/Microsoft.AspNetCore.Tests/WebApplicationTests.cs +++ b/src/DefaultBuilder/test/Microsoft.AspNetCore.Tests/WebApplicationTests.cs @@ -1982,6 +1982,86 @@ public void CanObserveDefaultServicesInServiceCollection() Assert.Contains(builder.Services, service => service.ServiceType == typeof(ILogger<>)); } + [Fact] + public async Task RegisterAuthMiddlewaresCorrectly() + { + var helloEndpointCalled = false; + var customMiddlewareExecuted = false; + var username = "foobar"; + + var builder = WebApplication.CreateBuilder(); + builder.Services.AddAuthorization(); + builder.Services.AddAuthentication("testSchemeName") + .AddScheme("testSchemeName", "testDisplayName", _ => { }); + builder.WebHost.UseTestServer(); + await using var app = builder.Build(); + + app.Use(next => + { + return async context => + { + // IAuthenticationFeature is added by the authentication middleware + // during invocation. This middleware should run after authentication + // and be able to access the feature. + var authFeature = context.Features.Get(); + Assert.NotNull(authFeature); + customMiddlewareExecuted = true; + Assert.Equal(username, context.User.Identity.Name); + await next(context); + }; + }); + + app.MapGet("/hello", (ClaimsPrincipal user) => + { + helloEndpointCalled = true; + Assert.Equal(username, user.Identity.Name); + }).AllowAnonymous(); + + await app.StartAsync(); + var client = app.GetTestClient(); + await client.GetStringAsync($"/hello?username={username}"); + + Assert.True(helloEndpointCalled); + Assert.True(customMiddlewareExecuted); + } + + [Fact] + public async Task SupportsDisablingMiddlewareAutoRegistration() + { + var builder = WebApplication.CreateBuilder(); + builder.Services.AddAuthorization(); + builder.Services.AddAuthentication("testSchemeName") + .AddScheme("testSchemeName", "testDisplayName", _ => { }); + builder.WebHost.UseTestServer(); + await using var app = builder.Build(); + + app.Use(next => + { + return async context => + { + // IAuthenticationFeature is added by the authentication middleware + // during invocation. This middleware should run after authentication + // and be able to access the feature. + var authFeature = context.Features.Get(); + Assert.Null(authFeature); + Assert.Null(context.User.Identity.Name); + await next(context); + }; + }); + + app.Properties["__AuthenticationMiddlewareSet"] = true; + + app.MapGet("/hello", (ClaimsPrincipal user) => {}).AllowAnonymous(); + + Assert.True(app.Properties.ContainsKey("__AuthenticationMiddlewareSet")); + Assert.False(app.Properties.ContainsKey("__AuthorizationMiddlewareSet")); + + await app.StartAsync(); + + Assert.True(app.Properties.ContainsKey("__AuthenticationMiddlewareSet")); + Assert.True(app.Properties.ContainsKey("__AuthorizationMiddlewareSet")); + } + private class UberHandler : AuthenticationHandler { public UberHandler(IOptionsMonitor options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock) : base(options, logger, encoder, clock) { } diff --git a/src/Http/Authentication.Core/src/AuthenticationCoreServiceCollectionExtensions.cs b/src/Http/Authentication.Core/src/AuthenticationCoreServiceCollectionExtensions.cs index fc844b0dae5a..e0b80c0517db 100644 --- a/src/Http/Authentication.Core/src/AuthenticationCoreServiceCollectionExtensions.cs +++ b/src/Http/Authentication.Core/src/AuthenticationCoreServiceCollectionExtensions.cs @@ -23,6 +23,7 @@ public static IServiceCollection AddAuthenticationCore(this IServiceCollection s throw new ArgumentNullException(nameof(services)); } + services.TryAddSingleton(); services.TryAddScoped(); services.TryAddSingleton(); // Can be replaced with scoped ones that use DbContext services.TryAddScoped(); diff --git a/src/Http/Authentication.Core/src/AuthenticationMarkerService.cs b/src/Http/Authentication.Core/src/AuthenticationMarkerService.cs new file mode 100644 index 000000000000..f4952868a534 --- /dev/null +++ b/src/Http/Authentication.Core/src/AuthenticationMarkerService.cs @@ -0,0 +1,8 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace Microsoft.AspNetCore.Authentication; + +internal sealed class AuthenticationMarkerService +{ +} diff --git a/src/Http/Authentication.Core/src/Microsoft.AspNetCore.Authentication.Core.csproj b/src/Http/Authentication.Core/src/Microsoft.AspNetCore.Authentication.Core.csproj index 5786fd9a889a..c60f007150f5 100644 --- a/src/Http/Authentication.Core/src/Microsoft.AspNetCore.Authentication.Core.csproj +++ b/src/Http/Authentication.Core/src/Microsoft.AspNetCore.Authentication.Core.csproj @@ -16,4 +16,8 @@ + + + + diff --git a/src/Security/Authentication/test/AuthenticationMiddlewareTests.cs b/src/Security/Authentication/test/AuthenticationMiddlewareTests.cs index 458c30f69288..d83f2e4a23f9 100644 --- a/src/Security/Authentication/test/AuthenticationMiddlewareTests.cs +++ b/src/Security/Authentication/test/AuthenticationMiddlewareTests.cs @@ -154,7 +154,7 @@ public async Task IAuthenticateResultFeature_SettingResultSetsUser() } [Fact] - public async Task WebApplicationBuilder_RegistersAuthenticationMiddlewares() + public async Task WebApplicationBuilder_RegistersAuthenticationAndAuthorizationMiddlewares() { var builder = WebApplication.CreateBuilder(); builder.Configuration.AddInMemoryCollection(new[] @@ -162,23 +162,41 @@ public async Task WebApplicationBuilder_RegistersAuthenticationMiddlewares() new KeyValuePair("Authentication:Schemes:Bearer:ClaimsIssuer", "SomeIssuer"), new KeyValuePair("Authentication:Schemes:Bearer:Audiences:0", "https://localhost:5001") }); + builder.Services.AddAuthorization(); builder.Services.AddAuthentication().AddJwtBearer(); await using var app = builder.Build(); // Authentication middleware isn't registered until application // is built on startup Assert.False(app.Properties.ContainsKey("__AuthenticationMiddlewareSet")); + Assert.False(app.Properties.ContainsKey("__AuthorizationMiddlewareSet")); await app.StartAsync(); - // TODO: Validate after restructuring auto-registration of middlewares. - // Assert.True(app.Properties.ContainsKey("__AuthenticationMiddlewareSet")); + Assert.True(app.Properties.ContainsKey("__AuthenticationMiddlewareSet")); + Assert.True(app.Properties.ContainsKey("__AuthorizationMiddlewareSet")); var options = app.Services.GetService>().Get(JwtBearerDefaults.AuthenticationScheme); Assert.Equal(new[] { "SomeIssuer" }, options.TokenValidationParameters.ValidIssuers); Assert.Equal(new[] { "https://localhost:5001" }, options.TokenValidationParameters.ValidAudiences); } + [Fact] + public async Task WebApplicationBuilder_OnlyRegistersMiddlewareWithSupportedServices() + { + var builder = WebApplication.CreateBuilder(); + builder.Services.AddAuthentication().AddJwtBearer(); + await using var app = builder.Build(); + + Assert.False(app.Properties.ContainsKey("__AuthenticationMiddlewareSet")); + Assert.False(app.Properties.ContainsKey("__AuthorizationMiddlewareSet")); + + await app.StartAsync(); + + Assert.True(app.Properties.ContainsKey("__AuthenticationMiddlewareSet")); + Assert.False(app.Properties.ContainsKey("__AuthorizationMiddlewareSet")); + } + private HttpContext GetHttpContext( Action registerServices = null, IAuthenticationService authenticationService = null) diff --git a/src/Security/Authorization/Policy/src/AuthorizationAppBuilderExtensions.cs b/src/Security/Authorization/Policy/src/AuthorizationAppBuilderExtensions.cs index 1e6cedfc5b18..74b23c01f871 100644 --- a/src/Security/Authorization/Policy/src/AuthorizationAppBuilderExtensions.cs +++ b/src/Security/Authorization/Policy/src/AuthorizationAppBuilderExtensions.cs @@ -12,6 +12,8 @@ namespace Microsoft.AspNetCore.Builder; /// public static class AuthorizationAppBuilderExtensions { + internal const string AuthorizationMiddlewareSetKey = "__AuthorizationMiddlewareSet"; + /// /// Adds the to the specified , which enables authorization capabilities. /// @@ -30,6 +32,7 @@ public static IApplicationBuilder UseAuthorization(this IApplicationBuilder app) VerifyServicesRegistered(app); + app.Properties[AuthorizationMiddlewareSetKey] = true; return app.UseMiddleware(); } diff --git a/src/Security/Authorization/Policy/src/Microsoft.AspNetCore.Authorization.Policy.csproj b/src/Security/Authorization/Policy/src/Microsoft.AspNetCore.Authorization.Policy.csproj index 443d62ff4c5f..cb6aa87c6ee8 100644 --- a/src/Security/Authorization/Policy/src/Microsoft.AspNetCore.Authorization.Policy.csproj +++ b/src/Security/Authorization/Policy/src/Microsoft.AspNetCore.Authorization.Policy.csproj @@ -21,4 +21,8 @@ + + + + From 55a076c6002ff6431b830c02d1e8f2484f7878f6 Mon Sep 17 00:00:00 2001 From: Safia Abdalla Date: Wed, 6 Jul 2022 13:39:38 -0700 Subject: [PATCH 3/5] Apply suggestions from code review Co-authored-by: David Fowler --- src/DefaultBuilder/src/WebApplicationBuilder.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/DefaultBuilder/src/WebApplicationBuilder.cs b/src/DefaultBuilder/src/WebApplicationBuilder.cs index 27b199d216fa..e27d1c9c2a3c 100644 --- a/src/DefaultBuilder/src/WebApplicationBuilder.cs +++ b/src/DefaultBuilder/src/WebApplicationBuilder.cs @@ -172,7 +172,7 @@ private void ConfigureApplication(WebHostBuilderContext context, IApplicationBui // Process authorization and authentication middlewares independently to avoid // registering middlewares for services that do not exist - if (_builtApplication.Services.GetService() != null) + if (_builtApplication.Services.GetService() is not null) { // Don't add more than one instance of the middleware if (!_builtApplication.Properties.ContainsKey(AuthenticationMiddlewareSetKey)) @@ -184,7 +184,7 @@ private void ConfigureApplication(WebHostBuilderContext context, IApplicationBui } } - if (_builtApplication.Services.GetService() != null) + if (_builtApplication.Services.GetService() is not null) { if (!_builtApplication.Properties.ContainsKey(AuthorizationMiddlewareSetKey)) { From 9c14f54cf82d6b99a978cf3ca4ec1dfab9afe548 Mon Sep 17 00:00:00 2001 From: Safia Abdalla Date: Wed, 6 Jul 2022 22:39:30 +0000 Subject: [PATCH 4/5] Remove IVT for world peace --- src/DefaultBuilder/src/WebApplicationBuilder.cs | 6 +++--- .../src/AuthenticationCoreServiceCollectionExtensions.cs | 1 - .../src/AuthenticationMarkerService.cs | 8 -------- .../src/Microsoft.AspNetCore.Authentication.Core.csproj | 4 ---- .../src/Microsoft.AspNetCore.Authorization.Policy.csproj | 4 ---- 5 files changed, 3 insertions(+), 20 deletions(-) delete mode 100644 src/Http/Authentication.Core/src/AuthenticationMarkerService.cs diff --git a/src/DefaultBuilder/src/WebApplicationBuilder.cs b/src/DefaultBuilder/src/WebApplicationBuilder.cs index e27d1c9c2a3c..9b79f395587f 100644 --- a/src/DefaultBuilder/src/WebApplicationBuilder.cs +++ b/src/DefaultBuilder/src/WebApplicationBuilder.cs @@ -3,7 +3,7 @@ using System.Diagnostics; using Microsoft.AspNetCore.Authentication; -using Microsoft.AspNetCore.Authorization.Policy; +using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; @@ -172,7 +172,7 @@ private void ConfigureApplication(WebHostBuilderContext context, IApplicationBui // Process authorization and authentication middlewares independently to avoid // registering middlewares for services that do not exist - if (_builtApplication.Services.GetService() is not null) + if (_builtApplication.Services.GetService() is not null) { // Don't add more than one instance of the middleware if (!_builtApplication.Properties.ContainsKey(AuthenticationMiddlewareSetKey)) @@ -184,7 +184,7 @@ private void ConfigureApplication(WebHostBuilderContext context, IApplicationBui } } - if (_builtApplication.Services.GetService() is not null) + if (_builtApplication.Services.GetService() is not null) { if (!_builtApplication.Properties.ContainsKey(AuthorizationMiddlewareSetKey)) { diff --git a/src/Http/Authentication.Core/src/AuthenticationCoreServiceCollectionExtensions.cs b/src/Http/Authentication.Core/src/AuthenticationCoreServiceCollectionExtensions.cs index e0b80c0517db..fc844b0dae5a 100644 --- a/src/Http/Authentication.Core/src/AuthenticationCoreServiceCollectionExtensions.cs +++ b/src/Http/Authentication.Core/src/AuthenticationCoreServiceCollectionExtensions.cs @@ -23,7 +23,6 @@ public static IServiceCollection AddAuthenticationCore(this IServiceCollection s throw new ArgumentNullException(nameof(services)); } - services.TryAddSingleton(); services.TryAddScoped(); services.TryAddSingleton(); // Can be replaced with scoped ones that use DbContext services.TryAddScoped(); diff --git a/src/Http/Authentication.Core/src/AuthenticationMarkerService.cs b/src/Http/Authentication.Core/src/AuthenticationMarkerService.cs deleted file mode 100644 index f4952868a534..000000000000 --- a/src/Http/Authentication.Core/src/AuthenticationMarkerService.cs +++ /dev/null @@ -1,8 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -namespace Microsoft.AspNetCore.Authentication; - -internal sealed class AuthenticationMarkerService -{ -} diff --git a/src/Http/Authentication.Core/src/Microsoft.AspNetCore.Authentication.Core.csproj b/src/Http/Authentication.Core/src/Microsoft.AspNetCore.Authentication.Core.csproj index c60f007150f5..5786fd9a889a 100644 --- a/src/Http/Authentication.Core/src/Microsoft.AspNetCore.Authentication.Core.csproj +++ b/src/Http/Authentication.Core/src/Microsoft.AspNetCore.Authentication.Core.csproj @@ -16,8 +16,4 @@ - - - - diff --git a/src/Security/Authorization/Policy/src/Microsoft.AspNetCore.Authorization.Policy.csproj b/src/Security/Authorization/Policy/src/Microsoft.AspNetCore.Authorization.Policy.csproj index cb6aa87c6ee8..443d62ff4c5f 100644 --- a/src/Security/Authorization/Policy/src/Microsoft.AspNetCore.Authorization.Policy.csproj +++ b/src/Security/Authorization/Policy/src/Microsoft.AspNetCore.Authorization.Policy.csproj @@ -21,8 +21,4 @@ - - - - From 08d17b8fd854a2d467204cba521e810157949b89 Mon Sep 17 00:00:00 2001 From: Safia Abdalla Date: Thu, 7 Jul 2022 03:34:45 +0000 Subject: [PATCH 5/5] Check for non-scoped service --- src/DefaultBuilder/src/WebApplicationBuilder.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/DefaultBuilder/src/WebApplicationBuilder.cs b/src/DefaultBuilder/src/WebApplicationBuilder.cs index 9b79f395587f..d002acd3a5e8 100644 --- a/src/DefaultBuilder/src/WebApplicationBuilder.cs +++ b/src/DefaultBuilder/src/WebApplicationBuilder.cs @@ -172,7 +172,7 @@ private void ConfigureApplication(WebHostBuilderContext context, IApplicationBui // Process authorization and authentication middlewares independently to avoid // registering middlewares for services that do not exist - if (_builtApplication.Services.GetService() is not null) + if (_builtApplication.Services.GetService() is not null) { // Don't add more than one instance of the middleware if (!_builtApplication.Properties.ContainsKey(AuthenticationMiddlewareSetKey))