Skip to content

Commit 0794d60

Browse files
authored
Make AuthenticateAsync return non-null result (#22708)
* Make AuthenticateAsync return non-null result * NoResult
1 parent a273560 commit 0794d60

File tree

5 files changed

+12
-10
lines changed

5 files changed

+12
-10
lines changed

src/Http/Authentication.Abstractions/ref/Microsoft.AspNetCore.Authentication.Abstractions.netcoreapp.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ protected AuthenticateResult() { }
2222
}
2323
public static partial class AuthenticationHttpContextExtensions
2424
{
25-
public static System.Threading.Tasks.Task<Microsoft.AspNetCore.Authentication.AuthenticateResult?> AuthenticateAsync(this Microsoft.AspNetCore.Http.HttpContext context) { throw null; }
26-
public static System.Threading.Tasks.Task<Microsoft.AspNetCore.Authentication.AuthenticateResult?> AuthenticateAsync(this Microsoft.AspNetCore.Http.HttpContext context, string? scheme) { throw null; }
25+
public static System.Threading.Tasks.Task<Microsoft.AspNetCore.Authentication.AuthenticateResult> AuthenticateAsync(this Microsoft.AspNetCore.Http.HttpContext context) { throw null; }
26+
public static System.Threading.Tasks.Task<Microsoft.AspNetCore.Authentication.AuthenticateResult> AuthenticateAsync(this Microsoft.AspNetCore.Http.HttpContext context, string? scheme) { throw null; }
2727
public static System.Threading.Tasks.Task ChallengeAsync(this Microsoft.AspNetCore.Http.HttpContext context) { throw null; }
2828
public static System.Threading.Tasks.Task ChallengeAsync(this Microsoft.AspNetCore.Http.HttpContext context, Microsoft.AspNetCore.Authentication.AuthenticationProperties? properties) { throw null; }
2929
public static System.Threading.Tasks.Task ChallengeAsync(this Microsoft.AspNetCore.Http.HttpContext context, string scheme) { throw null; }
@@ -157,7 +157,7 @@ public partial interface IAuthenticationSchemeProvider
157157
}
158158
public partial interface IAuthenticationService
159159
{
160-
System.Threading.Tasks.Task<Microsoft.AspNetCore.Authentication.AuthenticateResult?> AuthenticateAsync(Microsoft.AspNetCore.Http.HttpContext context, string? scheme);
160+
System.Threading.Tasks.Task<Microsoft.AspNetCore.Authentication.AuthenticateResult> AuthenticateAsync(Microsoft.AspNetCore.Http.HttpContext context, string? scheme);
161161
System.Threading.Tasks.Task ChallengeAsync(Microsoft.AspNetCore.Http.HttpContext context, string? scheme, Microsoft.AspNetCore.Authentication.AuthenticationProperties? properties);
162162
System.Threading.Tasks.Task ForbidAsync(Microsoft.AspNetCore.Http.HttpContext context, string? scheme, Microsoft.AspNetCore.Authentication.AuthenticationProperties? properties);
163163
System.Threading.Tasks.Task SignInAsync(Microsoft.AspNetCore.Http.HttpContext context, string? scheme, System.Security.Claims.ClaimsPrincipal principal, Microsoft.AspNetCore.Authentication.AuthenticationProperties? properties);

src/Http/Authentication.Abstractions/src/AuthenticationHttpContextExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public static class AuthenticationHttpContextExtensions
1818
/// </summary>
1919
/// <param name="context">The <see cref="HttpContext"/> context.</param>
2020
/// <returns>The <see cref="AuthenticateResult"/>.</returns>
21-
public static Task<AuthenticateResult?> AuthenticateAsync(this HttpContext context) =>
21+
public static Task<AuthenticateResult> AuthenticateAsync(this HttpContext context) =>
2222
context.AuthenticateAsync(scheme: null);
2323

2424
/// <summary>
@@ -27,7 +27,7 @@ public static class AuthenticationHttpContextExtensions
2727
/// <param name="context">The <see cref="HttpContext"/> context.</param>
2828
/// <param name="scheme">The name of the authentication scheme.</param>
2929
/// <returns>The <see cref="AuthenticateResult"/>.</returns>
30-
public static Task<AuthenticateResult?> AuthenticateAsync(this HttpContext context, string? scheme) =>
30+
public static Task<AuthenticateResult> AuthenticateAsync(this HttpContext context, string? scheme) =>
3131
context.RequestServices.GetRequiredService<IAuthenticationService>().AuthenticateAsync(context, scheme);
3232

3333
/// <summary>

src/Http/Authentication.Abstractions/src/IAuthenticationService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public interface IAuthenticationService
1818
/// <param name="context">The <see cref="HttpContext"/>.</param>
1919
/// <param name="scheme">The name of the authentication scheme.</param>
2020
/// <returns>The result.</returns>
21-
Task<AuthenticateResult?> AuthenticateAsync(HttpContext context, string? scheme);
21+
Task<AuthenticateResult> AuthenticateAsync(HttpContext context, string? scheme);
2222

2323
/// <summary>
2424
/// Challenge the specified authentication scheme.

src/Http/Authentication.Core/ref/Microsoft.AspNetCore.Authentication.Core.netcoreapp.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public AuthenticationService(Microsoft.AspNetCore.Authentication.IAuthentication
4040
public Microsoft.AspNetCore.Authentication.IAuthenticationSchemeProvider Schemes { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } }
4141
public Microsoft.AspNetCore.Authentication.IClaimsTransformation Transform { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } }
4242
[System.Diagnostics.DebuggerStepThroughAttribute]
43-
public virtual System.Threading.Tasks.Task<Microsoft.AspNetCore.Authentication.AuthenticateResult?> AuthenticateAsync(Microsoft.AspNetCore.Http.HttpContext context, string? scheme) { throw null; }
43+
public virtual System.Threading.Tasks.Task<Microsoft.AspNetCore.Authentication.AuthenticateResult> AuthenticateAsync(Microsoft.AspNetCore.Http.HttpContext context, string? scheme) { throw null; }
4444
[System.Diagnostics.DebuggerStepThroughAttribute]
4545
public virtual System.Threading.Tasks.Task ChallengeAsync(Microsoft.AspNetCore.Http.HttpContext context, string? scheme, Microsoft.AspNetCore.Authentication.AuthenticationProperties? properties) { throw null; }
4646
[System.Diagnostics.DebuggerStepThroughAttribute]

src/Http/Authentication.Core/src/AuthenticationService.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public AuthenticationService(IAuthenticationSchemeProvider schemes, IAuthenticat
5959
/// <param name="context">The <see cref="HttpContext"/>.</param>
6060
/// <param name="scheme">The name of the authentication scheme.</param>
6161
/// <returns>The result.</returns>
62-
public virtual async Task<AuthenticateResult?> AuthenticateAsync(HttpContext context, string? scheme)
62+
public virtual async Task<AuthenticateResult> AuthenticateAsync(HttpContext context, string? scheme)
6363
{
6464
if (scheme == null)
6565
{
@@ -77,8 +77,10 @@ public AuthenticationService(IAuthenticationSchemeProvider schemes, IAuthenticat
7777
throw await CreateMissingHandlerException(scheme);
7878
}
7979

80-
var result = await handler.AuthenticateAsync();
81-
if (result != null && result.Succeeded)
80+
// Handlers should not return null, but we'll be tolerant of null values for legacy reasons.
81+
var result = (await handler.AuthenticateAsync()) ?? AuthenticateResult.NoResult();
82+
83+
if (result.Succeeded)
8284
{
8385
var principal = result.Principal!;
8486
var doTransform = true;

0 commit comments

Comments
 (0)