IdentityModel.AspNetCore.OAuth2Introspection icon indicating copy to clipboard operation
IdentityModel.AspNetCore.OAuth2Introspection copied to clipboard

fix: fix PostConfigureOAuth2IntrospectionOptions with named options

Open NatMarchand opened this issue 3 years ago • 4 comments

Hi there! We are currently encountering a weird issue described as follow: Given I bind OAuth2IntrospectionOptions with an IConfiguration section and using a IOptionsMonitor<OAuth2IntrospectionOptions> somewhere When a configuration provider triggers a reload Then an exception occurs in the PostConfigureOAuth2IntrospectionOptions because dotnet tries to PostConfigure an unnamed options instance (which is of course not configured) and the message is "You must either set Authority or IntrospectionEndpoint"

Here's my repro (on LinqPad):

async Task Main()
{
	var configurationProvider = new MyConfigurationProvider();
	var builder = WebApplication.CreateBuilder();
	((IConfigurationBuilder)builder.Configuration).Add(configurationProvider);
	builder.Services
		.AddAuthentication()
		.AddOAuth2Introspection();

	builder.Services
		.AddOptions<OAuth2IntrospectionOptions>(OAuth2IntrospectionDefaults.AuthenticationScheme)
		.BindConfiguration("IdentityServer");

	var app = builder.Build();

	var monitor = app.Services.GetRequiredService<IOptionsMonitor<OAuth2IntrospectionOptions>>();
	monitor.Get("Bearer").Dump();

	configurationProvider.NotifyReload(); // <-- exception occurs here
}


public class MyConfigurationProvider : ConfigurationProvider, IConfigurationSource
{
	public IConfigurationProvider Build(IConfigurationBuilder builder)
	{
		return this;
	}

	public override void Load()
	{
		this.Data.Clear();
		this.Data.Add("IdentityServer:Authority", "http://dummy");
	}

	public void NotifyReload()
	{
		this.OnReload();
	}
}

My PR fixes this behavior by registering a named PostConfigureOAuth2IntrospectionOptions.

NatMarchand avatar Oct 24 '22 16:10 NatMarchand

Hello there ! Is there anything I can do to help you about this MR ? :)

NatMarchand avatar Nov 24 '22 17:11 NatMarchand

Are you seeing this under .NET 6 or .NET Core 3.1?

brockallen avatar Nov 28 '22 16:11 brockallen

The code above was tested on LinqPad with dotnet 7 but we also have issues with our apps running on dotnet 6

NatMarchand avatar Nov 28 '22 16:11 NatMarchand

I'd like to understand better why it's invoked without a name/scheme only when the source reloads, as opposed to normal startup (first time). Can you find out?

brockallen avatar Nov 28 '22 18:11 brockallen

We're in the process of migrating this library into the Duende Open Source repo over here. I'm closing this issue as we get ready to do that move, but if you would like to revisit the discussion, please open a new issue in that repository.

josephdecock avatar Jul 09 '25 17:07 josephdecock