Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
306 views
in Technique[技术] by (71.8m points)

c# - Configuring token in azure functions

Problem is that I get Functions runtime is unreachable error after adding AddAccessTokenManagement() in startup.cs file. Also the list of fuctions in azure is empty. The best part is that from app insights I see that my cron job is beeing executed anyway, and token is working. When running my code in local enviroment there is no problem reported, deployments also seems to be fine. This is how I configure my http client to work with identity token:

    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 => { });
    }

Worth to mention that this way of configuring it works with my Web API application.

Any ideas guys?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 => {});
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...