Skip to content
This repository has been archived by the owner on Jul 14, 2020. It is now read-only.

Get ServiceProvider inside AddOpenIdConnect without BuildServiceProvider #137

Closed
304NotModified opened this issue Jun 17, 2020 · 1 comment
Labels

Comments

@304NotModified
Copy link

304NotModified commented Jun 17, 2020

Question

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.

Minimal working example

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();

Note: For events like OnValidatePrincipal we could get it from CookieValidatePrincipalContext.HttpContext.RequestServices

We get also now this warning:

warning "Calling 'BuildServiceProvider' from application code results in a additional copy of Singleton services being created"

@304NotModified
Copy link
Author

Wrong repo! Oops!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

1 participant