Skip to content

Add support for reading default scheme from config #41987

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 13, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -32,10 +32,6 @@ public AuthenticationBuilder(IServiceCollection services)
where THandler : class, IAuthenticationHandler
{
var state = new AddSchemeHelperState(typeof(THandler));
// Don't read global authentication options until an
// authentication scheme has been configured to avoid
// prematurely registering options
Services.TryAddEnumerable(ServiceDescriptor.Singleton<IConfigureOptions<AuthenticationOptions>, AuthenticationConfigureOptions>());
Services.Configure<AuthenticationOptions>(o =>
{
o.AddScheme(authenticationScheme, scheme =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ public static class AuthenticationConfigurationProviderExtensions
/// <returns>The specified <see cref="IConfiguration"/> object, or null if the requested section does not exist.</returns>
public static IConfiguration GetSchemeConfiguration(this IAuthenticationConfigurationProvider provider, string authenticationScheme)
{
ArgumentNullException.ThrowIfNull(provider, nameof(provider));

if (provider.AuthenticationConfiguration is null)
{
throw new InvalidOperationException("There was no top-level authentication property found in configuration.");
}

return provider.AuthenticationConfiguration.GetSection($"{AuthenticationSchemesKey}:{authenticationScheme}");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using Microsoft.AspNetCore.Authentication;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;

namespace Microsoft.Extensions.DependencyInjection;

Expand All @@ -28,6 +29,8 @@ public static AuthenticationBuilder AddAuthentication(this IServiceCollection se
services.AddWebEncoders();
services.TryAddSingleton<ISystemClock, SystemClock>();
services.TryAddSingleton<IAuthenticationConfigurationProvider, DefaultAuthenticationConfigurationProvider>();
services.TryAddEnumerable(ServiceDescriptor.Singleton<IConfigureOptions<AuthenticationOptions>, AuthenticationConfigureOptions>());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also discuss configuration changes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah at a minimum we'd have to move away from IOptions and use IOptionsMonitor for AuthenticationOptions to be able to do that.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


return new AuthenticationBuilder(services);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

Expand All @@ -18,6 +19,7 @@ private Task<IHost> CreateHost(Action<TOptions> configureOptions, Func<HttpConte
=> CreateHostWithServices(s =>
{
var builder = s.AddAuthentication();
s.AddSingleton<IConfiguration>(new ConfigurationManager());
if (isDefault)
{
s.Configure<AuthenticationOptions>(o => o.DefaultScheme = DefaultScheme);
Expand Down