Access to 'provider' when calling AddAccessTokenManagement
flcdrg opened this issue · comments
David Gardiner commented
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
});
});
lastme commented
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);
})
Dominick Baier commented
@flcdrg please send a PR
David Gardiner commented
I'll see what I can come up with 😄
github-actions commented
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue.