C# 在azure函数中配置令牌

C# 在azure函数中配置令牌,c#,azure,azure-functions,token,C#,Azure,Azure Functions,Token,问题是,在startup.cs文件中添加AddAccessTokenManagement()后,我获取的函数运行时不可访问。azure中的功能列表也是空的。最棒的是,从appinsights中,我看到我的cron作业正在执行,而token正在工作。当在本地环境中运行我的代码时,并没有报告任何问题,部署似乎也很好。以下是我如何配置http客户端以使用标识令牌: private void ConfigureAccessToken(IFunctionsHostBuilder builder)

问题是,在startup.cs文件中添加AddAccessTokenManagement()后,我获取的函数运行时不可访问。azure中的功能列表也是空的。最棒的是,从appinsights中,我看到我的cron作业正在执行,而token正在工作。当在本地环境中运行我的代码时,并没有报告任何问题,部署似乎也很好。以下是我如何配置http客户端以使用标识令牌:

    private void ConfigureAccessToken(IFunctionsHostBuilder builder)
    {
        var IdentityServerUrl = "<serverUrl>"; ;

        builder.Services.AddHttpClient();
        builder.Services.AddAccessTokenManagement(options =>
        {
            options.Client.Clients.Add("cloud-service", new ClientCredentialsTokenRequest
            {
                Address = $"{IdentityServerUrl}/connect/token",
                ClientId = _authorizationConfig.ClientId,
                ClientSecret = _authorizationConfig.ClientSecret,
            });
        });
        builder.Services.AddClientAccessTokenClient("internal-client", configureClient: client => { });
    }
private void ConfigureAccessToken(IFunctionsHostBuilder)
{
var IdentityServerUrl=“”;
builder.Services.AddHttpClient();
builder.Services.AddAccessTokenManagement(选项=>
{
options.Client.Clients.Add(“云服务”,new ClientCredentialsTokenRequest
{
地址=$“{IdentityServerUrl}/connect/token”,
ClientId=\u authorizationConfig.ClientId,
ClientSecret=\u authorizationConfig.ClientSecret,
});
});
builder.Services.AddClientAccessTokenClient(“内部客户端”,configureClient:client=>{});
}
值得一提的是,这种配置方式适用于我的Web API应用程序


伙计们,有什么想法吗?

我自己找到了答案。azure函数的令牌确认似乎不同于Web API。工作代码如下:

    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 => {});
    }
private void ConfigureAccessToken(IFunctionsHostBuilder)
{
var IdentityServerUrl=“”;
builder.Services.Configure(o=>
{
o、 Client.Clients.Add(“云服务”,new ClientCredentialsTokenRequest
{
地址=$“{IdentityServerUrl}/connect/token”,
ClientId=\u authorizationConfig.ClientId,
ClientSecret=\u authorizationConfig.ClientSecret,
});
});
builder.Services.AddDistributedMemoryCache();
builder.Services.AddTransient(s=>
{
返回新的DefaultTokenClientConfigurationService(
s、 GetRequiredService(),
无效的
无效);
});
builder.Services.AddHttpClient(AccessTokenManagementDefaults.BackChannelHttpClientName);
builder.Services.TryAddTransient();
builder.Services.TryAddTransient();
builder.Services.AddTransient(s=>
{
返回新的AccessTokenManagementService(
无效的
无效的
s、 GetRequiredService(),
s、 GetRequiredService(),
s、 GetRequiredService(),
s、 GetRequiredService()
);
});
builder.Services.AddTransient();
builder.Services.AddClientAccessTokenClient(“内部客户端”,configureClient:config=>{});
}

请记住,Azure函数和传统的.net核心API的配置机制并不相同,因此您不应该期望它们的行为相同。下面是我对一个类似问题的回答,解释了它们的区别()。如果您需要更多帮助,我也可以在这里的答案中详细说明。关于如何处理配置的文章非常好,但我没有看到关于令牌的信息。这看起来像是很多奇怪的锅炉板代码。人们不禁要问,他们为什么把事情弄得如此复杂