Is there a good way to get the ServiceProvider in the AddOpenIdConnect, or configure the ClientSecret later where we have the DI container fully setup? (e.g. in Configure(IApplicationBuilder app))
We're getting the client secret from somewhere else and we like to use DI for that.
Currently we do this, but I really like to remove services.BuildServiceProvider()
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddOpenIdConnect(AuthenticationScheme, options =>
{
ServiceProvider serviceProvider = services.BuildServiceProvider(); // we like to prevent this
options.ClientSecret = serviceProvider.GetRequiredService<ISecretRetriever>().GetClientSecret();
Notes
For events like OnValidatePrincipal we could get it from CookieValidatePrincipalContext.HttpContext.RequestServices
Using services.BuildServiceProvider() will give this warning:
warning "Calling 'BuildServiceProvider' from application code results in a additional copy of Singleton services being created"