Search code examples
asp.netasp.net-identity

asp.net header forwarding not working for external Identity provider


I use asp.net Identity with AzureAD as an external Identity provider in my Balzor server side app. In development environment (localhost) logging in works fine. When I deploy the app to an on premise server in a docker image behind Nginx, it does not. Microsoft sends the error message AADSTS50011: The reply URL specified in the request does not match the reply URLs configured for the application. I have added the proper reply URL to Azure portal. As far as I can tell, the request uses http, while https should be used, which causes the error.

Since Nginx handles secure transport, the headers need to be forwarded, so I configured Nginx and enabled Header forwarding in Startup.ConfigureServices:

services.Configure<ForwardedHeadersOptions>(options =>
            {
                options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
                options.ForwardLimit = 1;
                options.KnownProxies.Add(IPAddress.Parse("123.xxx.xxx.xxx"));
            });

and at the very beginning of Startup.Configure:

app.UseForwardedHeaders();

app.UseHsts();

// should not be necessary but I tried with and without
//app.UseHttpsRedirection();

When I enable logging, I think I see that the correct header is forwarded from Nginx:

...
Header: X-Forwarded-For: 123.xxx.xxx.xxx
Header: X-Forwarded-Proto: https
...

To me it looks like ChallengeResult() in ExternalLogin.Post is not using the forwarded headers and sends http://my.domain.ch/signin-oidc instead of https:// as reply URL, which causes the error.

I ran out of ideas what else I could try, any suggestions please?


Solution

  • After some digging I found the mistake: I did add the wrong proxy IP. Since my asp.net app is hosted on docker, I had to add the IP address of the docker image as proxy, not the IP of the server which hosts nginx and docker. In fact, I added the default network used by docker

    options.KnownNetworks.Add(new IPNetwork(IPAddress.Parse("172.17.0.0"), 16));