C# interceptor for updating metadata headers

31 views
Skip to first unread message

Mark Nuttall-Smith

unread,
Dec 6, 2018, 5:02:26 AM12/6/18
to grpc.io
Hi,

I'd like to use a C# interceptor to transparently refresh an access token, similar to the System.Net.Http.DelegatingHandler here: https://github.com/IdentityModel/IdentityModel.OidcClient2/blob/v3/src/RefreshTokenDelegatingHandler.cs

My question is: is it possible to set a header in the call metadata using the server streaming call interceptor?

I also see the metadata interceptor, but this doesn't have the continuation, so I can't try and retry if I get a 401 unauthorized.
public static CallInvoker Intercept(this CallInvoker invoker, Func<Metadata, Metadata> interceptor)

TIA,
Mark

Mark Nuttall-Smith

unread,
Dec 7, 2018, 5:41:28 AM12/7/18
to grpc.io
Just looking at this again, and it is quite straightforward of course:

public override AsyncServerStreamingCall<TResponse> AsyncServerStreamingCall<TRequest, TResponse>(TRequest request, ClientInterceptorContext<TRequest, TResponse> context, AsyncServerStreamingCallContinuation<TRequest, TResponse> continuation)
{
    context
.Options.Headers.Add(CreateBearerTokenHeader("..."));
    return continuation(request, context);
}

Just need to figure out how to catch a 401 response, refresh the token and retry.... 

I've seen this one, but it would be great to have a few more examples of interceptor usage, particularly with streaming responses. Does anybody know of something?
Reply all
Reply to author
Forward
0 new messages