diff --git a/src/Microsoft.AspNetCore.Authentication.Core/AuthenticationSchemeProvider.cs b/src/Microsoft.AspNetCore.Authentication.Core/AuthenticationSchemeProvider.cs index e56247e1..325d4993 100644 --- a/src/Microsoft.AspNetCore.Authentication.Core/AuthenticationSchemeProvider.cs +++ b/src/Microsoft.AspNetCore.Authentication.Core/AuthenticationSchemeProvider.cs @@ -59,7 +59,7 @@ public Task GetDefaultAuthenticateSchemeAsync() /// /// Returns the scheme that will be used by default for . /// This is typically specified via . - /// 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 . /// /// The scheme that will be used by default for . public Task GetDefaultChallengeSchemeAsync() @@ -68,11 +68,7 @@ public Task GetDefaultChallengeSchemeAsync() { return GetSchemeAsync(_options.DefaultChallengeScheme); } - if (_map.Count == 1) - { - return Task.FromResult(_map.Values.First()); - } - return Task.FromResult(null); + return GetDefaultAuthenticateSchemeAsync(); } /// @@ -93,7 +89,7 @@ public Task GetDefaultForbidSchemeAsync() /// /// Returns the scheme that will be used by default for . /// This is typically specified via . - /// 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 . /// /// The scheme that will be used by default for . public Task GetDefaultSignInSchemeAsync() @@ -102,11 +98,7 @@ public Task GetDefaultSignInSchemeAsync() { return GetSchemeAsync(_options.DefaultSignInScheme); } - if (_map.Count == 1) - { - return Task.FromResult(_map.Values.First()); - } - return Task.FromResult(null); + return GetDefaultAuthenticateSchemeAsync(); } /// diff --git a/test/Microsoft.AspNetCore.Authentication.Core.Test/AuthenticationSchemeProviderTests.cs b/test/Microsoft.AspNetCore.Authentication.Core.Test/AuthenticationSchemeProviderTests.cs index 3810f833..9fe65bd2 100644 --- a/test/Microsoft.AspNetCore.Authentication.Core.Test/AuthenticationSchemeProviderTests.cs +++ b/test/Microsoft.AspNetCore.Authentication.Core.Test/AuthenticationSchemeProviderTests.cs @@ -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("A", "whatever"); + o.AddScheme("B", "whatever"); + }).BuildServiceProvider(); + + var provider = services.GetRequiredService(); + 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() {