I found the answer by myself. Looks like token confiuration for azure functions differ from Web API. Working code below:
private void ConfigureAccessToken(IFunctionsHostBuilder builder)
{
var IdentityServerUrl = "<serverUri>";
builder.Services.Configure<AccessTokenManagementOptions>(o =>
{
o.Client.Clients.Add("cloud-service", new ClientCredentialsTokenRequest
{
Address = $"{IdentityServerUrl}/connect/token",
ClientId = _authorizationConfig.ClientId,
ClientSecret = _authorizationConfig.ClientSecret,
});
});
builder.Services.AddDistributedMemoryCache();
builder.Services.AddTransient<ITokenClientConfigurationService, DefaultTokenClientConfigurationService>(s =>
{
return new DefaultTokenClientConfigurationService(
s.GetRequiredService<IOptions<AccessTokenManagementOptions>>(),
null,
null);
});
builder.Services.AddHttpClient(AccessTokenManagementDefaults.BackChannelHttpClientName);
builder.Services.TryAddTransient<ITokenEndpointService, TokenEndpointService>();
builder.Services.TryAddTransient<IClientAccessTokenCache, ClientAccessTokenCache>();
builder.Services.AddTransient<IAccessTokenManagementService, AccessTokenManagementService>(s =>
{
return new AccessTokenManagementService(
null,
null,
s.GetRequiredService<IOptions<AccessTokenManagementOptions>>(),
s.GetRequiredService<ITokenEndpointService>(),
s.GetRequiredService<IClientAccessTokenCache>(),
s.GetRequiredService<ILogger<AccessTokenManagementService>>()
);
});
builder.Services.AddTransient<ClientAccessTokenHandler>();
builder.Services.AddClientAccessTokenClient("internal-client", configureClient: config => {});
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…