From 0794d6077d53164b8a5206febac7c50ae358028b Mon Sep 17 00:00:00 2001 From: Pranav K Date: Tue, 9 Jun 2020 10:05:26 -0700 Subject: [PATCH] Make AuthenticateAsync return non-null result (#22708) * Make AuthenticateAsync return non-null result * NoResult --- ...t.AspNetCore.Authentication.Abstractions.netcoreapp.cs | 6 +++--- .../src/AuthenticationHttpContextExtensions.cs | 4 ++-- .../src/IAuthenticationService.cs | 2 +- ...Microsoft.AspNetCore.Authentication.Core.netcoreapp.cs | 2 +- src/Http/Authentication.Core/src/AuthenticationService.cs | 8 +++++--- 5 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/Http/Authentication.Abstractions/ref/Microsoft.AspNetCore.Authentication.Abstractions.netcoreapp.cs b/src/Http/Authentication.Abstractions/ref/Microsoft.AspNetCore.Authentication.Abstractions.netcoreapp.cs index 8a790d9883..2a4614b603 100644 --- a/src/Http/Authentication.Abstractions/ref/Microsoft.AspNetCore.Authentication.Abstractions.netcoreapp.cs +++ b/src/Http/Authentication.Abstractions/ref/Microsoft.AspNetCore.Authentication.Abstractions.netcoreapp.cs @@ -22,8 +22,8 @@ namespace Microsoft.AspNetCore.Authentication } public static partial class AuthenticationHttpContextExtensions { - public static System.Threading.Tasks.Task AuthenticateAsync(this Microsoft.AspNetCore.Http.HttpContext context) { throw null; } - public static System.Threading.Tasks.Task AuthenticateAsync(this Microsoft.AspNetCore.Http.HttpContext context, string? scheme) { throw null; } + public static System.Threading.Tasks.Task AuthenticateAsync(this Microsoft.AspNetCore.Http.HttpContext context) { throw null; } + public static System.Threading.Tasks.Task AuthenticateAsync(this Microsoft.AspNetCore.Http.HttpContext context, string? scheme) { throw null; } public static System.Threading.Tasks.Task ChallengeAsync(this Microsoft.AspNetCore.Http.HttpContext context) { throw null; } public static System.Threading.Tasks.Task ChallengeAsync(this Microsoft.AspNetCore.Http.HttpContext context, Microsoft.AspNetCore.Authentication.AuthenticationProperties? properties) { throw null; } public static System.Threading.Tasks.Task ChallengeAsync(this Microsoft.AspNetCore.Http.HttpContext context, string scheme) { throw null; } @@ -157,7 +157,7 @@ namespace Microsoft.AspNetCore.Authentication } public partial interface IAuthenticationService { - System.Threading.Tasks.Task AuthenticateAsync(Microsoft.AspNetCore.Http.HttpContext context, string? scheme); + System.Threading.Tasks.Task AuthenticateAsync(Microsoft.AspNetCore.Http.HttpContext context, string? scheme); System.Threading.Tasks.Task ChallengeAsync(Microsoft.AspNetCore.Http.HttpContext context, string? scheme, Microsoft.AspNetCore.Authentication.AuthenticationProperties? properties); System.Threading.Tasks.Task ForbidAsync(Microsoft.AspNetCore.Http.HttpContext context, string? scheme, Microsoft.AspNetCore.Authentication.AuthenticationProperties? properties); System.Threading.Tasks.Task SignInAsync(Microsoft.AspNetCore.Http.HttpContext context, string? scheme, System.Security.Claims.ClaimsPrincipal principal, Microsoft.AspNetCore.Authentication.AuthenticationProperties? properties); diff --git a/src/Http/Authentication.Abstractions/src/AuthenticationHttpContextExtensions.cs b/src/Http/Authentication.Abstractions/src/AuthenticationHttpContextExtensions.cs index e831311e6e..539f1c74f3 100644 --- a/src/Http/Authentication.Abstractions/src/AuthenticationHttpContextExtensions.cs +++ b/src/Http/Authentication.Abstractions/src/AuthenticationHttpContextExtensions.cs @@ -18,7 +18,7 @@ namespace Microsoft.AspNetCore.Authentication /// /// The context. /// The . - public static Task AuthenticateAsync(this HttpContext context) => + public static Task AuthenticateAsync(this HttpContext context) => context.AuthenticateAsync(scheme: null); /// @@ -27,7 +27,7 @@ namespace Microsoft.AspNetCore.Authentication /// The context. /// The name of the authentication scheme. /// The . - public static Task AuthenticateAsync(this HttpContext context, string? scheme) => + public static Task AuthenticateAsync(this HttpContext context, string? scheme) => context.RequestServices.GetRequiredService().AuthenticateAsync(context, scheme); /// diff --git a/src/Http/Authentication.Abstractions/src/IAuthenticationService.cs b/src/Http/Authentication.Abstractions/src/IAuthenticationService.cs index ab32aee2ef..14a53b6f4a 100644 --- a/src/Http/Authentication.Abstractions/src/IAuthenticationService.cs +++ b/src/Http/Authentication.Abstractions/src/IAuthenticationService.cs @@ -18,7 +18,7 @@ namespace Microsoft.AspNetCore.Authentication /// The . /// The name of the authentication scheme. /// The result. - Task AuthenticateAsync(HttpContext context, string? scheme); + Task AuthenticateAsync(HttpContext context, string? scheme); /// /// Challenge the specified authentication scheme. diff --git a/src/Http/Authentication.Core/ref/Microsoft.AspNetCore.Authentication.Core.netcoreapp.cs b/src/Http/Authentication.Core/ref/Microsoft.AspNetCore.Authentication.Core.netcoreapp.cs index ae1807281f..59ed390fc0 100644 --- a/src/Http/Authentication.Core/ref/Microsoft.AspNetCore.Authentication.Core.netcoreapp.cs +++ b/src/Http/Authentication.Core/ref/Microsoft.AspNetCore.Authentication.Core.netcoreapp.cs @@ -40,7 +40,7 @@ namespace Microsoft.AspNetCore.Authentication public Microsoft.AspNetCore.Authentication.IAuthenticationSchemeProvider Schemes { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } } public Microsoft.AspNetCore.Authentication.IClaimsTransformation Transform { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } } [System.Diagnostics.DebuggerStepThroughAttribute] - public virtual System.Threading.Tasks.Task AuthenticateAsync(Microsoft.AspNetCore.Http.HttpContext context, string? scheme) { throw null; } + public virtual System.Threading.Tasks.Task AuthenticateAsync(Microsoft.AspNetCore.Http.HttpContext context, string? scheme) { throw null; } [System.Diagnostics.DebuggerStepThroughAttribute] public virtual System.Threading.Tasks.Task ChallengeAsync(Microsoft.AspNetCore.Http.HttpContext context, string? scheme, Microsoft.AspNetCore.Authentication.AuthenticationProperties? properties) { throw null; } [System.Diagnostics.DebuggerStepThroughAttribute] diff --git a/src/Http/Authentication.Core/src/AuthenticationService.cs b/src/Http/Authentication.Core/src/AuthenticationService.cs index 56172f1739..61a11b47a4 100644 --- a/src/Http/Authentication.Core/src/AuthenticationService.cs +++ b/src/Http/Authentication.Core/src/AuthenticationService.cs @@ -59,7 +59,7 @@ namespace Microsoft.AspNetCore.Authentication /// The . /// The name of the authentication scheme. /// The result. - public virtual async Task AuthenticateAsync(HttpContext context, string? scheme) + public virtual async Task AuthenticateAsync(HttpContext context, string? scheme) { if (scheme == null) { @@ -77,8 +77,10 @@ namespace Microsoft.AspNetCore.Authentication throw await CreateMissingHandlerException(scheme); } - var result = await handler.AuthenticateAsync(); - if (result != null && result.Succeeded) + // Handlers should not return null, but we'll be tolerant of null values for legacy reasons. + var result = (await handler.AuthenticateAsync()) ?? AuthenticateResult.NoResult(); + + if (result.Succeeded) { var principal = result.Principal!; var doTransform = true;