IdentityModel / IdentityModel.AspNetCore

ASP.NET Core helper library for claims-based identity, OAuth 2.0 and OpenID Connect.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Access to 'provider' when calling AddAccessTokenManagement

flcdrg opened this issue · comments

When using AddAccessTokenManagement I'd really like to load the configuration values from a previously registered IOptions object. To do that I need access to an IServiceProvider.

For example when I register a HttpClient, I can do it like this:

services.AddOptions<ClientOptions>()
    .Bind(config.GetSection("ClientOptions"))
    .ValidateDataAnnotations();

services.AddSingleton(provider => provider.GetRequiredService<IOptions<ClientOptions>>().Value);

services.AddHttpClient("client"),
        (provider, client) =>
        {
            var options = provider.GetRequiredService<ClientOptions>();

            client.BaseAddress = new Uri(options.BaseUrl);
            client.DefaultRequestHeaders.Authorization =
                new BasicAuthenticationHeaderValue(options.Username, options.Password);
        })

I'd like to be able to do something similar to this with AddAccessTokenManagement eg.

services.AddAccessTokenManagement((provider, options) =>
{
    var cfg = provider.GetRequiredService<AuthOptions>();

    options.Client.Clients.Add("thing", new ClientCredentialsTokenRequest
    {
        Address = cfg.Address,
        ClientId = cfg.ClientId,
        ClientSecret = cfg.ClientSecret,
        Scope = cfg.Scope
    });
});

this is asp.net core 6, but should be similar:

builder.Services.AddAccessTokenManagement(options => {
        var request = new ClientCredentialsTokenRequest();
        builder.Configuration.Bind("AccessTokenService", request);
        options.Client.Clients.Add("apiclient", request);
    })

@flcdrg please send a PR

I'll see what I can come up with 😄

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue.