grpc-dotnet icon indicating copy to clipboard operation
grpc-dotnet copied to clipboard

Is it possible to access request metadata in CallCredentials

Open jakoss opened this issue 4 months ago • 3 comments

My use case is that i want to perform custom logic inside my CallCredentials implementation based on the metadata set manually during request creation.

So when i execute my gRPC call like this:

var response = await someService.ExecuteSomeMethod(new RequestClass(), headers: new Metadata
        {
            { "CustomData", "some data" }
        });

I would like to access the "CustomData" inside the CallCredentials, like this:

internal static async Task InterceptAuthentication(AuthInterceptorContext _, Metadata metadata, IServiceProvider provider)
    {
        var myCustomData = metadata["CustomData"];
    }

So far, the passed metadata is just empty and the documentation suggests that this is only for appending additional data. Right now i need to use very hacky AsyncLoca-based workaround for that. Is there a way to access the original metadata for the request here?

jakoss avatar Aug 20 '25 08:08 jakoss

Could you use Grpc.Core.Interceptors.Interceptor instead? It is possible to read and modify context there.

plachta11b avatar Aug 29 '25 16:08 plachta11b

I just moved from interceptor since I cannot perform await there and the official recommendation is to use CallCredentials. This whole design seems a bit weird

jakoss avatar Aug 30 '25 06:08 jakoss

Having the same issue here.

I use a AddCallsCredentials to insert some custom logic for authentication: namely request a token exchange under certain conditions. Now I need to tweak such logic to perform the token exchange based on some request parameters.

I am also surprised that Interceptor doesn't support asynchronous operations and that the delegate used to create a CallCredentials uses the Metadata parameter as an output when it could have simply returned a Task<Metadata> to avoid ambiguity.

Kralizek avatar Sep 22 '25 14:09 Kralizek