Last active
April 28, 2020 08:33
-
-
Save xtrmstep/5a25f462f8a289d4803b87efad443285 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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