grpc / grpc-dotnet

gRPC for .NET
Apache License 2.0
4.17k stars 767 forks source link

AddDownstreamWebApi support or corresponding method #1898

Open SWarnberg opened 1 year ago

SWarnberg commented 1 year ago

Describe the solution you'd like

For OBO flows there's an excellent extension method when an API calls another API, AddDownstreamWebApi. However, even though gRPC clients use HttpClient internally, this extension doesn't seem to work. It would be great to get a similar extension, or same, to work with gRPC clients in ASP.NET Core.

Describe alternatives you've considered

Today we're using AddCallCredentials with code for getting the token manually using ITokenAcquisition.

Additional context

Referred scenario: A web API that calls web APIs Current solution: Bearer token with gRPC client factory

cfox-dev commented 1 year ago

I was trying to deep-dive into this topic and a quick solution might be extending the GrpcHttpClientBuilderExtensions with a new method more or less like this:

public static IHttpClientBuilder AddDownstreamWebApi(this IHttpClientBuilder builder, string serviceName,
        DownstreamWebApiOptions configuration)
{
        builder.EnableTokenAcquisitionToCallDownstreamApi();

    builder.Services.Configure<DownstreamWebApiOptions>(serviceName, configuration);
    builder.Services.AddHttpClient<IDownstreamWebApi, DownstreamWebApi>();

        return builder;
}

but this means that the Grpc.Net.ClientFactory project should contain a dependency to Microsoft.Identity.Web and not sure if this should be the case.