isaacmcn isaacmcn - 1 year ago 112
C# Question

Replacing default HttpClient for introspection in IdentityServer3

I'm using IdentityServer3 in a microservices based system that we are building to run on Service Fabric on premise. I'm wanting to know whether it is possible to replace the HttpClient instance that is used to call the introspection endpoint from within one of our Web API stateless services.

With service fabric you have the ability to call directly to other services within a cluster but to do that you need to first query the service registry to find out the specific url for the service (it could be on any node in the cluster). There are patterns/classes to do this but you need to wrap retry loops around it in case the service moves to a different node, the node goes offline, etc.

The alternative is to call out to a load balancer / API Gateway which contains the lookup/retry logic but I'd prefer to not incur this extra hop if possible.

Any ideas/help would be greatly appreciated!

Answer Source

I am guessing it for the token validation you are referencing, and looking at the options you indeed can set a HttpMessageHandler for doing the request.

You can then implement the SendAsync part of a messagehandler to change the url for which the request is being send.

Quick Example:

public class MessageHandler1 : DelegatingHandler
    protected override Task<HttpResponseMessage> SendAsync(
        HttpRequestMessage request, CancellationToken cancellationToken)
        //Update the request url by replacing the base address to the internal endpoint.
        return base.SendAsync(request, cancellationToken);

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download