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

Fallback to DefaultAuthenticateScheme #872

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public Task<AuthenticationScheme> GetDefaultAuthenticateSchemeAsync()
/// <summary>
/// Returns the scheme that will be used by default for <see cref="IAuthenticationService.ChallengeAsync(HttpContext, string, AuthenticationProperties)"/>.
/// This is typically specified via <see cref="AuthenticationOptions.DefaultChallengeScheme"/>.
/// Otherwise, if only a single scheme exists, that will be used, if more than one exists, null will be returned.
/// Otherwise, this will fallback to <see cref="GetDefaultAuthenticateSchemeAsync"/>.
/// </summary>
/// <returns>The scheme that will be used by default for <see cref="IAuthenticationService.ChallengeAsync(HttpContext, string, AuthenticationProperties)"/>.</returns>
public Task<AuthenticationScheme> GetDefaultChallengeSchemeAsync()
Expand All @@ -68,11 +68,7 @@ public Task<AuthenticationScheme> GetDefaultChallengeSchemeAsync()
{
return GetSchemeAsync(_options.DefaultChallengeScheme);
}
if (_map.Count == 1)
{
return Task.FromResult(_map.Values.First());
}
return Task.FromResult<AuthenticationScheme>(null);
return GetDefaultAuthenticateSchemeAsync();
}

/// <summary>
Expand All @@ -93,7 +89,7 @@ public Task<AuthenticationScheme> GetDefaultForbidSchemeAsync()
/// <summary>
/// Returns the scheme that will be used by default for <see cref="IAuthenticationService.SignInAsync(HttpContext, string, System.Security.Claims.ClaimsPrincipal, AuthenticationProperties)"/>.
/// This is typically specified via <see cref="AuthenticationOptions.DefaultSignInScheme"/>.
/// Otherwise, if only a single scheme exists, that will be used, if more than one exists, null will be returned.
/// Otherwise, this will fallback to <see cref="GetDefaultAuthenticateSchemeAsync"/>.
/// </summary>
/// <returns>The scheme that will be used by default for <see cref="IAuthenticationService.SignInAsync(HttpContext, string, System.Security.Claims.ClaimsPrincipal, AuthenticationProperties)"/>.</returns>
public Task<AuthenticationScheme> GetDefaultSignInSchemeAsync()
Expand All @@ -102,11 +98,7 @@ public Task<AuthenticationScheme> GetDefaultSignInSchemeAsync()
{
return GetSchemeAsync(_options.DefaultSignInScheme);
}
if (_map.Count == 1)
{
return Task.FromResult(_map.Values.First());
}
return Task.FromResult<AuthenticationScheme>(null);
return GetDefaultAuthenticateSchemeAsync();
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,24 @@ public async Task DefaultSchemesFallbackToOnlyScheme()
Assert.Equal("single", (await provider.GetDefaultSignOutSchemeAsync()).Name);
}

[Fact]
public async Task DefaultSchemesFallbackToAuthenticateScheme()
{
var services = new ServiceCollection().AddOptions().AddAuthenticationCore(o =>
{
o.DefaultAuthenticateScheme = "B";
o.AddScheme<Handler>("A", "whatever");
o.AddScheme<Handler>("B", "whatever");
}).BuildServiceProvider();

var provider = services.GetRequiredService<IAuthenticationSchemeProvider>();
Assert.Equal("B", (await provider.GetDefaultForbidSchemeAsync()).Name);
Assert.Equal("B", (await provider.GetDefaultAuthenticateSchemeAsync()).Name);
Assert.Equal("B", (await provider.GetDefaultChallengeSchemeAsync()).Name);
Assert.Equal("B", (await provider.GetDefaultSignInSchemeAsync()).Name);
Assert.Equal("B", (await provider.GetDefaultSignOutSchemeAsync()).Name);
}

[Fact]
public async Task DefaultSchemesAreSet()
{
Expand Down