diff --git a/src/Microsoft.AspNetCore.Authentication.Core/AuthenticationService.cs b/src/Microsoft.AspNetCore.Authentication.Core/AuthenticationService.cs index 54bdd82d..9a8223d0 100644 --- a/src/Microsoft.AspNetCore.Authentication.Core/AuthenticationService.cs +++ b/src/Microsoft.AspNetCore.Authentication.Core/AuthenticationService.cs @@ -113,11 +113,11 @@ public virtual async Task ForbidAsync(HttpContext context, string scheme, Authen { if (scheme == null) { - var defaultChallengeScheme = await Schemes.GetDefaultChallengeSchemeAsync(); - scheme = defaultChallengeScheme?.Name; + var defaultForbidScheme = await Schemes.GetDefaultForbidSchemeAsync(); + scheme = defaultForbidScheme?.Name; if (scheme == null) { - throw new InvalidOperationException($"No authenticationScheme was specified, and there was no DefaultChallengeScheme found."); + throw new InvalidOperationException($"No authenticationScheme was specified, and there was no DefaultForbidScheme found."); } } diff --git a/test/Microsoft.AspNetCore.Authentication.Core.Test/AuthenticationServiceTests.cs b/test/Microsoft.AspNetCore.Authentication.Core.Test/AuthenticationServiceTests.cs index c9fe57d9..292c56f5 100644 --- a/test/Microsoft.AspNetCore.Authentication.Core.Test/AuthenticationServiceTests.cs +++ b/test/Microsoft.AspNetCore.Authentication.Core.Test/AuthenticationServiceTests.cs @@ -122,6 +122,20 @@ public async Task ServicesWithDefaultSignOutMethodsTest() await Assert.ThrowsAsync(() => context.SignInAsync(new ClaimsPrincipal())); } + [Fact] + public async Task ServicesWithDefaultForbidMethod_CallsForbidMethod() + { + var services = new ServiceCollection().AddOptions().AddAuthenticationCore(o => + { + o.AddScheme("forbid", "whatever"); + o.DefaultForbidScheme = "forbid"; + }).BuildServiceProvider(); + var context = new DefaultHttpContext(); + context.RequestServices = services; + + await context.ForbidAsync(); + } + private class BaseHandler : IAuthenticationHandler { @@ -245,5 +259,43 @@ public Task SignOutAsync(AuthenticationProperties properties) } } + private class ForbidHandler : IAuthenticationHandler, IAuthenticationRequestHandler, IAuthenticationSignInHandler, IAuthenticationSignOutHandler + { + public Task AuthenticateAsync() + { + throw new NotImplementedException(); + } + + public Task ChallengeAsync(AuthenticationProperties properties) + { + throw new NotImplementedException(); + } + + public Task ForbidAsync(AuthenticationProperties properties) + { + return Task.FromResult(0); + } + + public Task HandleRequestAsync() + { + throw new NotImplementedException(); + } + + public Task InitializeAsync(AuthenticationScheme scheme, HttpContext context) + { + return Task.FromResult(0); + } + + public Task SignInAsync(ClaimsPrincipal user, AuthenticationProperties properties) + { + throw new NotImplementedException(); + } + + public Task SignOutAsync(AuthenticationProperties properties) + { + throw new NotImplementedException(); + } + } + } }