YARP authentication: how to refresh auth token

396 views Asked by At

YARP-based service(just YARP) proxies a request to Service1. And what is important YARP authenticates on Service1. What is the best way to implement token refresh (see scheme)? In middleware? Another way? Would be grateful for an example.

scheme

I tried to do something like this:

endpoints.MapReverseProxy(proxyPipeline =>
{
    proxyPipeline.Use((context, next) =>
    {
      await _next(context);

      if (context.Response.StatusCode == StatusCodes.Status401Unauthorized)
      {
        // requset NEW_Yarp-Service1_Token on Service1
        // set "Bearer NEW_Yarp-Service1_Token" to Header

        await _next(context);
      }
    });
    proxyPipeline.UseSessionAffinity();
    proxyPipeline.UseLoadBalancing();
    proxyPipeline.UsePassiveHealthChecks();
}); 

This leads to 502 Bad Gateway.

0

There are 0 answers