Skip to content

Commit

Permalink
Fix check for auth services in middleware auto-registration (dotnet#4…
Browse files Browse the repository at this point in the history
  • Loading branch information
captainsafia authored Aug 11, 2022
1 parent 10d6f49 commit efb694c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/DefaultBuilder/src/Microsoft.AspNetCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

<ItemGroup>
<InternalsVisibleTo Include="Microsoft.AspNetCore.Authentication.Test" />
<InternalsVisibleTo Include="Microsoft.AspNetCore.Authorization.Test" />
</ItemGroup>

</Project>
5 changes: 3 additions & 2 deletions src/DefaultBuilder/src/WebApplicationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ 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<IAuthenticationSchemeProvider>() is not null)
var serviceProviderIsService = _builtApplication.Services.GetService<IServiceProviderIsService>();
if (serviceProviderIsService?.IsService(typeof(IAuthenticationSchemeProvider)) is true)
{
// Don't add more than one instance of the middleware
if (!_builtApplication.Properties.ContainsKey(AuthenticationMiddlewareSetKey))
Expand All @@ -184,7 +185,7 @@ private void ConfigureApplication(WebHostBuilderContext context, IApplicationBui
}
}

if (_builtApplication.Services.GetService<IAuthorizationHandlerProvider>() is not null)
if (serviceProviderIsService?.IsService(typeof(IAuthorizationHandlerProvider)) is true)
{
if (!_builtApplication.Properties.ContainsKey(AuthorizationMiddlewareSetKey))
{
Expand Down
29 changes: 29 additions & 0 deletions src/Security/Authorization/test/AuthorizationMiddlewareTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization.Policy;
using Microsoft.AspNetCore.Authorization.Test.TestObjects;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Moq;

namespace Microsoft.AspNetCore.Authorization.Test;
Expand Down Expand Up @@ -658,6 +661,27 @@ public async Task IAuthenticateResultFeature_UsesExistingFeatureAndResult_Withou
Assert.Same(authenticateResult, authenticateResultFeature.AuthenticateResult);
}

// Validation for https://github.com/dotnet/aspnetcore/issues/43188
[Fact]
public async Task WebApplicationBuilder_CanRegisterAuthzMiddlewareWithScopedService()
{
var builder = WebApplication.CreateBuilder(new WebApplicationOptions()
{
EnvironmentName = Environments.Development
});
builder.Services.AddAuthorization();
builder.Services.AddScoped<IAuthorizationHandler, TestAuthorizationHandler>();
using var app = builder.Build();

Assert.False(app.Properties.ContainsKey("__AuthenticationMiddlewareSet"));
Assert.False(app.Properties.ContainsKey("__AuthorizationMiddlewareSet"));

await app.StartAsync();

Assert.False(app.Properties.ContainsKey("__AuthenticationMiddlewareSet"));
Assert.True(app.Properties.ContainsKey("__AuthorizationMiddlewareSet"));
}

private AuthorizationMiddleware CreateMiddleware(RequestDelegate requestDelegate = null, IAuthorizationPolicyProvider policyProvider = null)
{
requestDelegate = requestDelegate ?? ((context) => Task.CompletedTask);
Expand Down Expand Up @@ -724,6 +748,11 @@ private HttpContext GetHttpContext(
return httpContext;
}

public class TestAuthorizationHandler : IAuthorizationHandler
{
public Task HandleAsync(AuthorizationHandlerContext context) => Task.CompletedTask;
}

private class TestRequestDelegate
{
private readonly int _statusCode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
</PropertyGroup>

<ItemGroup>
<Reference Include="Microsoft.AspNetCore" />
<Reference Include="Microsoft.AspNetCore.Authorization" />
<Reference Include="Microsoft.AspNetCore.Authorization.Policy" />
<Reference Include="Microsoft.AspNetCore.Http" />
Expand Down

0 comments on commit efb694c

Please sign in to comment.