From 27b0f60f09f5b8a91a1d0706c3a2ec94ee00eee9 Mon Sep 17 00:00:00 2001 From: Justin Kotalik Date: Sat, 19 Aug 2017 22:09:22 -0700 Subject: [PATCH] ForbidAsync now uses correct Schemes method (#918) * ForbidAsync now uses correct Schemes method * comment * adds tests --- .../AuthenticationService.cs | 6 +-- .../AuthenticationServiceTests.cs | 52 +++++++++++++++++++ 2 files changed, 55 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.AspNetCore.Authentication.Core/AuthenticationService.cs b/src/Microsoft.AspNetCore.Authentication.Core/AuthenticationService.cs index 54bdd82d5a..9a8223d013 100644 --- a/src/Microsoft.AspNetCore.Authentication.Core/AuthenticationService.cs +++ b/src/Microsoft.AspNetCore.Authentication.Core/AuthenticationService.cs @@ -113,11 +113,11 @@ namespace Microsoft.AspNetCore.Authentication { 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 c9fe57d970..292c56f50c 100644 --- a/test/Microsoft.AspNetCore.Authentication.Core.Test/AuthenticationServiceTests.cs +++ b/test/Microsoft.AspNetCore.Authentication.Core.Test/AuthenticationServiceTests.cs @@ -122,6 +122,20 @@ namespace Microsoft.AspNetCore.Authentication 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 @@ namespace Microsoft.AspNetCore.Authentication } } + 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(); + } + } + } }