Skip to content

Commit 3fdbc2e

Browse files
authored
Merge pull request #26634 from dotnet-maestro-bot/merge/release/5.0-to-master
[automated] Merge branch 'release/5.0' => 'master'
2 parents d7c08a0 + 3f456b4 commit 3fdbc2e

Some content is hidden

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

42 files changed

+487
-64
lines changed

src/Middleware/Diagnostics/src/ExceptionHandler/ExceptionHandlerMiddleware.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ private async Task HandleException(HttpContext context, ExceptionDispatchInfo ed
134134

135135
await _options.ExceptionHandler!(context);
136136

137-
if (context.Response.StatusCode != StatusCodes.Status404NotFound)
137+
if (context.Response.StatusCode != StatusCodes.Status404NotFound || _options.AllowStatusCode404Response)
138138
{
139139
if (_diagnosticListener.IsEnabled() && _diagnosticListener.IsEnabled("Microsoft.AspNetCore.Diagnostics.HandledException"))
140140
{

src/Middleware/Diagnostics/src/ExceptionHandler/ExceptionHandlerOptions.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,14 @@ public class ExceptionHandlerOptions
2424
/// explicitly provided, the subsequent middleware pipeline will be used by default.
2525
/// </summary>
2626
public RequestDelegate? ExceptionHandler { get; set; }
27+
28+
/// <summary>
29+
/// This value controls whether the <see cref="ExceptionHandlerMiddleware" /> should
30+
/// consider a response with a 404 status code to be a valid result of executing the
31+
/// <see cref="ExceptionHandler"/>. The default value is false and the middleware will
32+
/// consider 404 status codes to be an error on the server and will therefore rethrow
33+
/// the original exception.
34+
/// </summary>
35+
public bool AllowStatusCode404Response { get; set; }
2736
}
2837
}

src/Middleware/Diagnostics/test/UnitTests/ExceptionHandlerTest.cs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,5 +542,59 @@ public async Task ExceptionHandlerNotFound_RethrowsOriginalError()
542542
&& w.EventId == 4
543543
&& w.Message == "No exception handler was found, rethrowing original exception.");
544544
}
545+
546+
[Fact]
547+
public async Task ExceptionHandler_CanReturn404Responses_WhenAllowed()
548+
{
549+
var sink = new TestSink(TestSink.EnableWithTypeName<ExceptionHandlerMiddleware>);
550+
var loggerFactory = new TestLoggerFactory(sink, enabled: true);
551+
552+
using var host = new HostBuilder()
553+
.ConfigureWebHost(webHostBuilder =>
554+
{
555+
webHostBuilder
556+
.UseTestServer()
557+
.ConfigureServices(services =>
558+
{
559+
services.AddSingleton<ILoggerFactory>(loggerFactory);
560+
services.Configure<ExceptionHandlerOptions>(options =>
561+
{
562+
options.AllowStatusCode404Response = true;
563+
options.ExceptionHandler = httpContext =>
564+
{
565+
httpContext.Response.StatusCode = StatusCodes.Status404NotFound;
566+
return Task.CompletedTask;
567+
};
568+
});
569+
})
570+
.Configure(app =>
571+
{
572+
app.UseExceptionHandler();
573+
574+
app.Map("/throw", (innerAppBuilder) =>
575+
{
576+
innerAppBuilder.Run(httpContext =>
577+
{
578+
throw new InvalidOperationException("Something bad happened.");
579+
});
580+
});
581+
});
582+
}).Build();
583+
584+
await host.StartAsync();
585+
586+
using (var server = host.GetTestServer())
587+
{
588+
var client = server.CreateClient();
589+
var response = await client.GetAsync("throw");
590+
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
591+
Assert.Equal(string.Empty, await response.Content.ReadAsStringAsync());
592+
}
593+
594+
Assert.DoesNotContain(sink.Writes, w =>
595+
w.LogLevel == LogLevel.Warning
596+
&& w.EventId == 4
597+
&& w.Message == "No exception handler was found, rethrowing original exception.");
598+
}
545599
}
546600
}

src/Security/Authentication/MicrosoftAccount/src/Microsoft.AspNetCore.Authentication.MicrosoftAccount.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<Description>ASP.NET Core middleware that enables an application to support the Microsoft Account authentication workflow.</Description>

src/Security/Authentication/Negotiate/src/Events/AuthenticatedContext.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ public class AuthenticatedContext : ResultContext<NegotiateOptions>
1313
/// <summary>
1414
/// Creates a new <see cref="AuthenticatedContext"/>.
1515
/// </summary>
16-
/// <param name="context"></param>
17-
/// <param name="scheme"></param>
18-
/// <param name="options"></param>
16+
/// <inheritdoc />
1917
public AuthenticatedContext(
2018
HttpContext context,
2119
AuthenticationScheme scheme,

src/Security/Authentication/Negotiate/src/Events/AuthenticationFailedContext.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ public class AuthenticationFailedContext : RemoteAuthenticationContext<Negotiate
1414
/// <summary>
1515
/// Creates a <see cref="AuthenticationFailedContext"/>.
1616
/// </summary>
17-
/// <param name="context"></param>
18-
/// <param name="scheme"></param>
19-
/// <param name="options"></param>
17+
/// <inheritdoc />
2018
public AuthenticationFailedContext(
2119
HttpContext context,
2220
AuthenticationScheme scheme,

src/Security/Authentication/Negotiate/src/Events/ChallengeContext.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@ public class ChallengeContext : PropertiesContext<NegotiateOptions>
1414
/// <summary>
1515
/// Creates a new <see cref="ChallengeContext"/>.
1616
/// </summary>
17-
/// <param name="context"></param>
18-
/// <param name="scheme"></param>
19-
/// <param name="options"></param>
20-
/// <param name="properties"></param>
17+
/// <inheritdoc />
2118
public ChallengeContext(
2219
HttpContext context,
2320
AuthenticationScheme scheme,
@@ -26,7 +23,8 @@ public ChallengeContext(
2623
: base(context, scheme, options, properties) { }
2724

2825
/// <summary>
29-
/// If true, will skip any default logic for this challenge.
26+
/// Gets a value that determines if this challenge was handled.
27+
/// If <see langword="true"/>, will skip any default logic for this challenge.
3028
/// </summary>
3129
public bool Handled { get; private set; }
3230

src/Security/Authentication/Negotiate/src/Events/LdapContext.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@ public class LdapContext : ResultContext<NegotiateOptions>
1313
/// <summary>
1414
/// Creates a new <see cref="LdapContext"/>.
1515
/// </summary>
16-
/// <param name="context"></param>
17-
/// <param name="scheme"></param>
18-
/// <param name="options"></param>
19-
/// <param name="settings"></param>
16+
/// <inheritdoc />
2017
public LdapContext(
2118
HttpContext context,
2219
AuthenticationScheme scheme,

src/Security/Authentication/Negotiate/src/LdapSettings.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ public class LdapSettings
7676

7777
internal MemoryCache ClaimsCache { get; set; }
7878

79+
/// <summary>
80+
/// Validates the <see cref="LdapSettings"/>.
81+
/// </summary>
7982
public void Validate()
8083
{
8184
if (EnableLdapClaimResolution)

src/Security/Authentication/Negotiate/src/Microsoft.AspNetCore.Authentication.Negotiate.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<PropertyGroup>
44
<Description>ASP.NET Core authentication handler used to authenticate requests using Negotiate, Kerberos, or NTLM.</Description>
55
<TargetFramework>$(DefaultNetCoreTargetFramework)</TargetFramework>
6+
<NoWarn>$(NoWarn.Replace('1591', ''))</NoWarn>
67
<GenerateDocumentationFile>true</GenerateDocumentationFile>
78
<PackageTags>aspnetcore;authentication;security</PackageTags>
89
</PropertyGroup>

0 commit comments

Comments
 (0)