Following the documentation at https://docs.microsoft.com/fr-fr/aspnet/core/grpc/authn-and-authz?view=aspnetcore-3.0#bearer-token-authentication, I try to configure my GRPC client to automatically inject a JWT token.
private static GrpcChannel CreateAuthenticatedChannel(string address)
{
var _token = "xxx"; //using some hardcoded JWT token for testing
var credentials = CallCredentials.FromInterceptor((context, metadata) =>
{
if (!string.IsNullOrEmpty(_token))
{
metadata.Add("Authorization", $"Bearer {_token}");
}
return Task.CompletedTask;
});
var channel = GrpcChannel.ForAddress(address, new GrpcChannelOptions
{
Credentials = ChannelCredentials.Create(ChannelCredentials.Insecure, credentials)
});
return channel;
}
But it chokes on an exception "Supplied channel credentials do not allow composition" in Grpc.Core
System.ArgumentException
HResult=0x80070057
Message=Supplied channel credentials do not allow composition.
Source=Grpc.Core.Api
StackTrace:
at Grpc.Core.Utils.GrpcPreconditions.CheckArgument(Boolean condition, String errorMessage)
at Grpc.Core.ChannelCredentials.CompositeChannelCredentials..ctor(ChannelCredentials channelCredentials, CallCredentials callCredentials)
at Grpc.Core.ChannelCredentials.Create(ChannelCredentials channelCredentials, CallCredentials callCredentials)
at Service2.StartupCommon.CreateAuthenticatedChannel(String address) in xxx\Service2\StartupCommon.cs:line 32
What does this mean ? How am I suppose to provide the HTTP Authorization header ?