首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在azure函数中配置令牌

在azure函数中配置令牌
EN

Stack Overflow用户
提问于 2020-12-24 08:36:59
回答 1查看 370关注 0票数 1

问题是,在将AddAccessTokenManagement()添加到startup.cs文件后,运行时是不可访问的错误。此外,蓝色中的函数列表是空的。最棒的是,从应用程序的洞察力中,我看到我的cron工作无论如何都在执行,而token正在工作。当在本地环境中运行我的代码时,报告中没有问题,部署似乎也很好。以下是我如何配置我的http客户端来使用标识令牌:

代码语言:javascript
复制
    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应用程序。

伙计们有什么想法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-01-04 09:36:10

我自己找到了答案。看起来,对于azure函数的令牌信任与Web不同。工作守则如下:

代码语言:javascript
复制
    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 => {});
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65435888

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档