Skip to content

Commit efb694c

Browse files
authored
Fix check for auth services in middleware auto-registration (#43205)
1 parent 10d6f49 commit efb694c

File tree

4 files changed

+34
-2
lines changed

4 files changed

+34
-2
lines changed

src/DefaultBuilder/src/Microsoft.AspNetCore.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636

3737
<ItemGroup>
3838
<InternalsVisibleTo Include="Microsoft.AspNetCore.Authentication.Test" />
39+
<InternalsVisibleTo Include="Microsoft.AspNetCore.Authorization.Test" />
3940
</ItemGroup>
4041

4142
</Project>

src/DefaultBuilder/src/WebApplicationBuilder.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,8 @@ private void ConfigureApplication(WebHostBuilderContext context, IApplicationBui
172172

173173
// Process authorization and authentication middlewares independently to avoid
174174
// registering middlewares for services that do not exist
175-
if (_builtApplication.Services.GetService<IAuthenticationSchemeProvider>() is not null)
175+
var serviceProviderIsService = _builtApplication.Services.GetService<IServiceProviderIsService>();
176+
if (serviceProviderIsService?.IsService(typeof(IAuthenticationSchemeProvider)) is true)
176177
{
177178
// Don't add more than one instance of the middleware
178179
if (!_builtApplication.Properties.ContainsKey(AuthenticationMiddlewareSetKey))
@@ -184,7 +185,7 @@ private void ConfigureApplication(WebHostBuilderContext context, IApplicationBui
184185
}
185186
}
186187

187-
if (_builtApplication.Services.GetService<IAuthorizationHandlerProvider>() is not null)
188+
if (serviceProviderIsService?.IsService(typeof(IAuthorizationHandlerProvider)) is true)
188189
{
189190
if (!_builtApplication.Properties.ContainsKey(AuthorizationMiddlewareSetKey))
190191
{

src/Security/Authorization/test/AuthorizationMiddlewareTests.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55
using Microsoft.AspNetCore.Authentication;
66
using Microsoft.AspNetCore.Authorization.Policy;
77
using Microsoft.AspNetCore.Authorization.Test.TestObjects;
8+
using Microsoft.AspNetCore.Builder;
9+
using Microsoft.AspNetCore.Hosting;
810
using Microsoft.AspNetCore.Http;
911
using Microsoft.Extensions.DependencyInjection;
12+
using Microsoft.Extensions.Hosting;
1013
using Moq;
1114

1215
namespace Microsoft.AspNetCore.Authorization.Test;
@@ -658,6 +661,27 @@ public async Task IAuthenticateResultFeature_UsesExistingFeatureAndResult_Withou
658661
Assert.Same(authenticateResult, authenticateResultFeature.AuthenticateResult);
659662
}
660663

664+
// Validation for https://github.com/dotnet/aspnetcore/issues/43188
665+
[Fact]
666+
public async Task WebApplicationBuilder_CanRegisterAuthzMiddlewareWithScopedService()
667+
{
668+
var builder = WebApplication.CreateBuilder(new WebApplicationOptions()
669+
{
670+
EnvironmentName = Environments.Development
671+
});
672+
builder.Services.AddAuthorization();
673+
builder.Services.AddScoped<IAuthorizationHandler, TestAuthorizationHandler>();
674+
using var app = builder.Build();
675+
676+
Assert.False(app.Properties.ContainsKey("__AuthenticationMiddlewareSet"));
677+
Assert.False(app.Properties.ContainsKey("__AuthorizationMiddlewareSet"));
678+
679+
await app.StartAsync();
680+
681+
Assert.False(app.Properties.ContainsKey("__AuthenticationMiddlewareSet"));
682+
Assert.True(app.Properties.ContainsKey("__AuthorizationMiddlewareSet"));
683+
}
684+
661685
private AuthorizationMiddleware CreateMiddleware(RequestDelegate requestDelegate = null, IAuthorizationPolicyProvider policyProvider = null)
662686
{
663687
requestDelegate = requestDelegate ?? ((context) => Task.CompletedTask);
@@ -724,6 +748,11 @@ private HttpContext GetHttpContext(
724748
return httpContext;
725749
}
726750

751+
public class TestAuthorizationHandler : IAuthorizationHandler
752+
{
753+
public Task HandleAsync(AuthorizationHandlerContext context) => Task.CompletedTask;
754+
}
755+
727756
private class TestRequestDelegate
728757
{
729758
private readonly int _statusCode;

src/Security/Authorization/test/Microsoft.AspNetCore.Authorization.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
</PropertyGroup>
66

77
<ItemGroup>
8+
<Reference Include="Microsoft.AspNetCore" />
89
<Reference Include="Microsoft.AspNetCore.Authorization" />
910
<Reference Include="Microsoft.AspNetCore.Authorization.Policy" />
1011
<Reference Include="Microsoft.AspNetCore.Http" />

0 commit comments

Comments
 (0)