Skip to content

Commit 53b9175

Browse files
authored
Register AuthN/Z middleware automatically in CreateSlimBuilder (#47664)
WebApplication.CreateSlimBuilder() should auto-register AuthN/Z middleware when associated services are added This is consistent with CreateBuilder, and doesn't add much size to the app when AuthN/Z are not used. Fix #47507
1 parent 0899b0a commit 53b9175

File tree

2 files changed

+32
-59
lines changed

2 files changed

+32
-59
lines changed

src/DefaultBuilder/src/WebApplicationBuilder.cs

Lines changed: 25 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,8 @@ internal WebApplicationBuilder(WebApplicationOptions options, bool slim, Action<
137137
{
138138
AspNetCore.WebHost.ConfigureWebDefaultsCore(webHostBuilder);
139139

140-
webHostBuilder.Configure(ConfigureEmptyApplication);
140+
// Runs inline.
141+
webHostBuilder.Configure(ConfigureApplication);
141142

142143
webHostBuilder.UseSetting(WebHostDefaults.ApplicationKey, _hostApplicationBuilder.Environment.ApplicationName ?? "");
143144
webHostBuilder.UseSetting(WebHostDefaults.PreventHostingStartupKey, Configuration[WebHostDefaults.PreventHostingStartupKey]);
@@ -260,46 +261,6 @@ public WebApplication Build()
260261
}
261262

262263
private void ConfigureApplication(WebHostBuilderContext context, IApplicationBuilder app)
263-
{
264-
ConfigureApplicationCore(
265-
context,
266-
app,
267-
processAuthMiddlewares: () =>
268-
{
269-
Debug.Assert(_builtApplication is not null);
270-
271-
// Process authorization and authentication middlewares independently to avoid
272-
// registering middlewares for services that do not exist
273-
var serviceProviderIsService = _builtApplication.Services.GetService<IServiceProviderIsService>();
274-
if (serviceProviderIsService?.IsService(typeof(IAuthenticationSchemeProvider)) is true)
275-
{
276-
// Don't add more than one instance of the middleware
277-
if (!_builtApplication.Properties.ContainsKey(AuthenticationMiddlewareSetKey))
278-
{
279-
// The Use invocations will set the property on the outer pipeline,
280-
// but we want to set it on the inner pipeline as well.
281-
_builtApplication.Properties[AuthenticationMiddlewareSetKey] = true;
282-
app.UseAuthentication();
283-
}
284-
}
285-
286-
if (serviceProviderIsService?.IsService(typeof(IAuthorizationHandlerProvider)) is true)
287-
{
288-
if (!_builtApplication.Properties.ContainsKey(AuthorizationMiddlewareSetKey))
289-
{
290-
_builtApplication.Properties[AuthorizationMiddlewareSetKey] = true;
291-
app.UseAuthorization();
292-
}
293-
}
294-
});
295-
}
296-
297-
private void ConfigureEmptyApplication(WebHostBuilderContext context, IApplicationBuilder app)
298-
{
299-
ConfigureApplicationCore(context, app, processAuthMiddlewares: null);
300-
}
301-
302-
private void ConfigureApplicationCore(WebHostBuilderContext context, IApplicationBuilder app, Action? processAuthMiddlewares)
303264
{
304265
Debug.Assert(_builtApplication is not null);
305266

@@ -340,7 +301,29 @@ private void ConfigureApplicationCore(WebHostBuilderContext context, IApplicatio
340301
}
341302
}
342303

343-
processAuthMiddlewares?.Invoke();
304+
// Process authorization and authentication middlewares independently to avoid
305+
// registering middlewares for services that do not exist
306+
var serviceProviderIsService = _builtApplication.Services.GetService<IServiceProviderIsService>();
307+
if (serviceProviderIsService?.IsService(typeof(IAuthenticationSchemeProvider)) is true)
308+
{
309+
// Don't add more than one instance of the middleware
310+
if (!_builtApplication.Properties.ContainsKey(AuthenticationMiddlewareSetKey))
311+
{
312+
// The Use invocations will set the property on the outer pipeline,
313+
// but we want to set it on the inner pipeline as well.
314+
_builtApplication.Properties[AuthenticationMiddlewareSetKey] = true;
315+
app.UseAuthentication();
316+
}
317+
}
318+
319+
if (serviceProviderIsService?.IsService(typeof(IAuthorizationHandlerProvider)) is true)
320+
{
321+
if (!_builtApplication.Properties.ContainsKey(AuthorizationMiddlewareSetKey))
322+
{
323+
_builtApplication.Properties[AuthorizationMiddlewareSetKey] = true;
324+
app.UseAuthorization();
325+
}
326+
}
344327

345328
// Wire the source pipeline to run in the destination pipeline
346329
app.Use(next =>

src/DefaultBuilder/test/Microsoft.AspNetCore.Tests/WebApplicationTests.cs

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2260,31 +2260,21 @@ public void CanObserveDefaultServicesInServiceCollection(CreateBuilderFunc creat
22602260
}
22612261

22622262
[Theory]
2263-
[InlineData(true)]
2264-
[InlineData(false)]
2265-
public async Task RegisterAuthMiddlewaresCorrectly(bool useSlimBuilder)
2263+
[MemberData(nameof(CreateBuilderFuncs))]
2264+
public async Task RegisterAuthMiddlewaresCorrectly(CreateBuilderFunc createBuilder)
22662265
{
22672266
var helloEndpointCalled = false;
22682267
var customMiddlewareExecuted = false;
22692268
var username = "foobar";
22702269

2271-
var builder = useSlimBuilder ?
2272-
WebApplication.CreateSlimBuilder() :
2273-
WebApplication.CreateBuilder();
2270+
var builder = createBuilder();
22742271

22752272
builder.Services.AddAuthorization();
22762273
builder.Services.AddAuthentication("testSchemeName")
22772274
.AddScheme<AuthenticationSchemeOptions, UberHandler>("testSchemeName", "testDisplayName", _ => { });
22782275
builder.WebHost.UseTestServer();
22792276
await using var app = builder.Build();
22802277

2281-
if (useSlimBuilder)
2282-
{
2283-
// NOTE: CreateSlimBuilder doesn't support auto registration of auth middleware, so need to do it explicitly
2284-
app.UseAuthentication();
2285-
app.UseAuthorization();
2286-
}
2287-
22882278
app.Use(next =>
22892279
{
22902280
return async context =>
@@ -2314,11 +2304,11 @@ public async Task RegisterAuthMiddlewaresCorrectly(bool useSlimBuilder)
23142304
Assert.True(customMiddlewareExecuted);
23152305
}
23162306

2317-
[Fact]
2318-
public async Task SupportsDisablingMiddlewareAutoRegistration()
2307+
[Theory]
2308+
[MemberData(nameof(CreateBuilderFuncs))]
2309+
public async Task SupportsDisablingMiddlewareAutoRegistration(CreateBuilderFunc createBuilder)
23192310
{
2320-
// NOTE: CreateSlimBuilder doesn't support auto registration of auth middleware
2321-
var builder = WebApplication.CreateBuilder();
2311+
var builder = createBuilder();
23222312
builder.Services.AddAuthorization();
23232313
builder.Services.AddAuthentication("testSchemeName")
23242314
.AddScheme<AuthenticationSchemeOptions, UberHandler>("testSchemeName", "testDisplayName", _ => { });

0 commit comments

Comments
 (0)