Setting Identity Server 4 Url Behind A Load Balancer

  • identityserver
  • c#

One of the problems of having an Identity Server behind a Load Balancer is to get the Discovery Document to show the correct urls. This post shows a solution with a custom Middleware to assign the proper url to the discovery endpoint.

The Discovery Endpoint Url Problem

When Identity Server host is bootstrapped as below, it runs on localhost and all Urls in the discovery endpoint has “localhost” in them.


Console.Title = "IdentityServer";

var host = new WebHostBuilder()


Discovery Endpoint output :


    "issuer": "http://localhost:5000",
    "jwks_uri": "http://localhost:5000/.well-known/openid-configuration/jwks",
    "authorization_endpoint": "http://localhost:5000/connect/authorize",
    "token_endpoint": "http://localhost:5000/connect/token",
    "userinfo_endpoint": "http://localhost:5000/connect/userinfo",
    "end_session_endpoint": "http://localhost:5000/connect/endsession",
    "check_session_iframe": "http://localhost:5000/connect/checksession",
    "revocation_endpoint": "http://localhost:5000/connect/revocation",
    "introspection_endpoint": "http://localhost:5000/connect/introspect",
    "frontchannel_logout_supported": true,
    "frontchannel_logout_session_supported": true,
    "REST OF IT"

If this setup is kept behind a load balancer, with a domain name of, the Urls will still have localhost in them. This could be okay if you don’t use the discovery document. But in many cases you have to. (Especially that oidc-client.js is the ideal way to bring token support to frontend and it uses discovery endpoint to identify the urls).

We cannot configure in the VM, because the VM, staying behind the load balancer, does not own that domain name. Infact the load balancer does.

Using Middleware to Solve it

We can write a simple custom middleware to feed the correct Url to the discovery endpoint.


using IdentityServer4.Extensions;
using Microsoft.AspNetCore.Http;
using System.Threading.Tasks;

namespace MyIdentityServer.Config
    /// <summary>
    /// Configures the HttpContext by assigning IdentityServerOrigin.
    /// </summary>
    public class PublicFacingUrlMiddleware
        private readonly RequestDelegate _next;
        private readonly string _publicFacingUri;

        public PublicFacingUrlMiddleware(RequestDelegate next, string publicFacingUri)
            _publicFacingUri = publicFacingUri;
            _next = next;

        public async Task Invoke(HttpContext context)
            var request = context.Request;


            await _next(context);

This middleware sets one important property in the Identity Server. The Origin. That’s used in generating the discovery document.

The last change is to use the new middleware in the IdentityServer pipeline in Startup.cs.

Startup.cs (where identityserver is added to the pipeline)

//app.UseIdentityServer(); This was replaced by the following 4 lines.

Now the PublicFacingUrlMiddleware is in the pipeline. You can add a configuration in appSettings.json to configure it to what you want. In my case it is


  "IdentityServerPublicFacingUri":  "",
  "FileLogging": {
    "IncludeScopes": false,
    "PathFormat": "Logs/log-{Date}.txt",
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information"

Load the Disovery Endpoint now and you will have it as follows.


    "issuer": "",
    "jwks_uri": "",
    "authorization_endpoint": "",
    "token_endpoint": "",
    "userinfo_endpoint": "",
    "end_session_endpoint": "",
    "check_session_iframe": "",
    "revocation_endpoint": "",
    "introspection_endpoint": "",

oidc-client.js should be now happy to take care of the frontend.


Written on April 28, 2017