ForbidAsync now uses correct Schemes method (#918)
* ForbidAsync now uses correct Schemes method * comment * adds tests
This commit is contained in:
parent
594f55947f
commit
27b0f60f09
|
|
@ -113,11 +113,11 @@ namespace Microsoft.AspNetCore.Authentication
|
||||||
{
|
{
|
||||||
if (scheme == null)
|
if (scheme == null)
|
||||||
{
|
{
|
||||||
var defaultChallengeScheme = await Schemes.GetDefaultChallengeSchemeAsync();
|
var defaultForbidScheme = await Schemes.GetDefaultForbidSchemeAsync();
|
||||||
scheme = defaultChallengeScheme?.Name;
|
scheme = defaultForbidScheme?.Name;
|
||||||
if (scheme == null)
|
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.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -122,6 +122,20 @@ namespace Microsoft.AspNetCore.Authentication
|
||||||
await Assert.ThrowsAsync<InvalidOperationException>(() => context.SignInAsync(new ClaimsPrincipal()));
|
await Assert.ThrowsAsync<InvalidOperationException>(() => context.SignInAsync(new ClaimsPrincipal()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task ServicesWithDefaultForbidMethod_CallsForbidMethod()
|
||||||
|
{
|
||||||
|
var services = new ServiceCollection().AddOptions().AddAuthenticationCore(o =>
|
||||||
|
{
|
||||||
|
o.AddScheme<ForbidHandler>("forbid", "whatever");
|
||||||
|
o.DefaultForbidScheme = "forbid";
|
||||||
|
}).BuildServiceProvider();
|
||||||
|
var context = new DefaultHttpContext();
|
||||||
|
context.RequestServices = services;
|
||||||
|
|
||||||
|
await context.ForbidAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private class BaseHandler : IAuthenticationHandler
|
private class BaseHandler : IAuthenticationHandler
|
||||||
{
|
{
|
||||||
|
|
@ -245,5 +259,43 @@ namespace Microsoft.AspNetCore.Authentication
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private class ForbidHandler : IAuthenticationHandler, IAuthenticationRequestHandler, IAuthenticationSignInHandler, IAuthenticationSignOutHandler
|
||||||
|
{
|
||||||
|
public Task<AuthenticateResult> AuthenticateAsync()
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task ChallengeAsync(AuthenticationProperties properties)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task ForbidAsync(AuthenticationProperties properties)
|
||||||
|
{
|
||||||
|
return Task.FromResult(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task<bool> 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue