Skip to content
This repository was archived by the owner on Nov 20, 2018. It is now read-only.

Commit ec3da99

Browse files
committed
ForbidAsync now uses correct Schemes method (#918)
* ForbidAsync now uses correct Schemes method * comment * adds tests
1 parent 7a1f70d commit ec3da99

File tree

2 files changed

+55
-3
lines changed

2 files changed

+55
-3
lines changed

src/Microsoft.AspNetCore.Authentication.Core/AuthenticationService.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,11 @@ public virtual async Task ForbidAsync(HttpContext context, string scheme, Authen
113113
{
114114
if (scheme == null)
115115
{
116-
var defaultChallengeScheme = await Schemes.GetDefaultChallengeSchemeAsync();
117-
scheme = defaultChallengeScheme?.Name;
116+
var defaultForbidScheme = await Schemes.GetDefaultForbidSchemeAsync();
117+
scheme = defaultForbidScheme?.Name;
118118
if (scheme == null)
119119
{
120-
throw new InvalidOperationException($"No authenticationScheme was specified, and there was no DefaultChallengeScheme found.");
120+
throw new InvalidOperationException($"No authenticationScheme was specified, and there was no DefaultForbidScheme found.");
121121
}
122122
}
123123

test/Microsoft.AspNetCore.Authentication.Core.Test/AuthenticationServiceTests.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,20 @@ public async Task ServicesWithDefaultSignOutMethodsTest()
122122
await Assert.ThrowsAsync<InvalidOperationException>(() => context.SignInAsync(new ClaimsPrincipal()));
123123
}
124124

125+
[Fact]
126+
public async Task ServicesWithDefaultForbidMethod_CallsForbidMethod()
127+
{
128+
var services = new ServiceCollection().AddOptions().AddAuthenticationCore(o =>
129+
{
130+
o.AddScheme<ForbidHandler>("forbid", "whatever");
131+
o.DefaultForbidScheme = "forbid";
132+
}).BuildServiceProvider();
133+
var context = new DefaultHttpContext();
134+
context.RequestServices = services;
135+
136+
await context.ForbidAsync();
137+
}
138+
125139

126140
private class BaseHandler : IAuthenticationHandler
127141
{
@@ -245,5 +259,43 @@ public Task SignOutAsync(AuthenticationProperties properties)
245259
}
246260
}
247261

262+
private class ForbidHandler : IAuthenticationHandler, IAuthenticationRequestHandler, IAuthenticationSignInHandler, IAuthenticationSignOutHandler
263+
{
264+
public Task<AuthenticateResult> AuthenticateAsync()
265+
{
266+
throw new NotImplementedException();
267+
}
268+
269+
public Task ChallengeAsync(AuthenticationProperties properties)
270+
{
271+
throw new NotImplementedException();
272+
}
273+
274+
public Task ForbidAsync(AuthenticationProperties properties)
275+
{
276+
return Task.FromResult(0);
277+
}
278+
279+
public Task<bool> HandleRequestAsync()
280+
{
281+
throw new NotImplementedException();
282+
}
283+
284+
public Task InitializeAsync(AuthenticationScheme scheme, HttpContext context)
285+
{
286+
return Task.FromResult(0);
287+
}
288+
289+
public Task SignInAsync(ClaimsPrincipal user, AuthenticationProperties properties)
290+
{
291+
throw new NotImplementedException();
292+
}
293+
294+
public Task SignOutAsync(AuthenticationProperties properties)
295+
{
296+
throw new NotImplementedException();
297+
}
298+
}
299+
248300
}
249301
}

0 commit comments

Comments
 (0)