IdentityServer / IdentityServer4.AccessTokenValidation

IdentityServer Access Token Validation for ASP.NET Core
Apache License 2.0
544 stars 214 forks source link

Get ServiceProvider inside AddOpenIdConnect without BuildServiceProvider #137

Closed 304NotModified closed 4 years ago

304NotModified commented 4 years ago

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 commented 4 years ago

Wrong repo! Oops!