【问题标题】:Configuring token in azure functions在 azure 函数中配置令牌
【发布时间】:2020-12-24 08:36:59
【问题描述】:

问题是在 startup.cs 文件中添加 AddAccessTokenManagement() 后,我得到 Functions runtime is unreachable 错误。天蓝色的功能列表也是空的。最好的部分是,从应用程序洞察力中,我看到我的 cron 工作无论如何都在执行,并且令牌正在工作。在本地环境中运行我的代码时,没有报告任何问题,部署似乎也很好。这就是我配置我的 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 => { });
    }

值得一提的是,这种配置方式适用于我的 Web API 应用程序。

各位有什么想法吗?

【问题讨论】:

  • 请记住,Azure 函数的配置机制和传统的 .net 核心 API 并不相同,因此您不应期望它们的行为相同。这是我对解释差异的类似问题的回答 (stackoverflow.com/questions/62960764/…)。如果您需要更多帮助,我也可以在这里的答案中说明。
  • 非常好的文章如何处理配置,但我没有看到关于令牌的信息。

标签: c# azure azure-functions token


【解决方案1】:

我自己找到了答案。看起来天蓝色函数的令牌配置与 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 => {});
    }

【讨论】:

  • 这看起来像很多奇怪的样板代码。有人想知道为什么他们把事情搞得这么复杂
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-07-30
  • 2023-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多