Let's say I have an HTTPClient configured in a custom FunctionsStartup class to be used inside an Azure Activity function, and I wanted that client to piggyback on IndentityModel's authentication token management, is there a reliable way to do this? I tried looking for solutions such as this one:
public override void Configure(IFunctionsHostBuilder builder)
{
// Config setup code removed for brevity
var identitySettings = _config
.GetSection("AuthenticationConfiguration")
.Get<AuthenticationConfiguration>();
// Configure token management
builder.Services.AddAccessTokenManagement(options =>
{
options.Client.Clients.Add("auth", new ClientCredentialsTokenRequest
{
Address = $"{identitySettings.Url}/connect/token",
ClientId = identitySettings.ClientId,
ClientSecret = identitySettings.ClientSecret,
Scope = identitySettings.Scopes[0]
});
});
// Piggyback token management to HTTPClient
builder.Services.AddHttpClient<IService, Service>(x =>
{
var settings = _config
.GetSection("Configuration")
.Get<Configuration>();
x.BaseAddress = new Uri(settings.Url);
}).AddClientAccessTokenHandler("auth");
}
But it doesn't work because Azure function's IServiceCollection is different from ASP.NET Core's IServiceCollection.
I also looked at these links but that doesn't still answer the question.
Figrued it out. You need to install
IdentityModelandIdentityModel.AspNetCorepackages from NuGet as they expose the methods needed to setup automated token management.More info