有没有办法在 Azure Activity 函数中使用 IdentityModel 通过 HTTP 客户端进行自动令牌管理?
Is there a way to use IdentityModel for automated token manegement with HTTP Clients inside Azure Activity Functions?
假设我在自定义 FunctionsStartup
class 中配置了一个 HTTPClient
以在 Azure Activity 函数中使用,并且我希望该客户端搭载IndentityModel
的身份验证令牌管理,有可靠的方法吗?我试着寻找这样的解决方案:
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");
}
但它不起作用,因为 Azure 函数的 IServiceCollection
不同于 ASP.NET Core 的 IServiceCollection
。
我也看过 links 但这仍然不能回答问题。
想通了。您需要从 NuGet 安装 IdentityModel
and IdentityModel.AspNetCore
包,因为它们公开了设置自动令牌管理所需的方法。
假设我在自定义 FunctionsStartup
class 中配置了一个 HTTPClient
以在 Azure Activity 函数中使用,并且我希望该客户端搭载IndentityModel
的身份验证令牌管理,有可靠的方法吗?我试着寻找这样的解决方案:
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");
}
但它不起作用,因为 Azure 函数的 IServiceCollection
不同于 ASP.NET Core 的 IServiceCollection
。
我也看过
想通了。您需要从 NuGet 安装 IdentityModel
and IdentityModel.AspNetCore
包,因为它们公开了设置自动令牌管理所需的方法。