Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save xtrmstep/5a25f462f8a289d4803b87efad443285 to your computer and use it in GitHub Desktop.
Save xtrmstep/5a25f462f8a289d4803b87efad443285 to your computer and use it in GitHub Desktop.
public class AuthHeadersInterceptor : Interceptor
{
public AuthHeadersInterceptor(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
}
public override AsyncUnaryCall<TResponse> AsyncUnaryCall<TRequest, TResponse>(TRequest request, ClientInterceptorContext<TRequest, TResponse> context, AsyncUnaryCallContinuation<TRequest, TResponse> continuation)
{
var metadata = new Metadata
{
{HttpHeaderNames.Authorization, $"Bearer <JWT_TOKEN>"}
};
var userIdentity = _httpContextAccessor.HttpContext.User.Identity;
if (userIdentity.IsAuthenticated)
{
metadata.Add(HttpHeaderNames.User, userIdentity.Name);
}
var callOption = context.Options.WithHeaders(metadata);
context = new ClientInterceptorContext<TRequest, TResponse>(context.Method, context.Host, callOption);
return base.AsyncUnaryCall(request, context, continuation);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment