aspnet / BasicMiddleware

[Archived] Basic middleware components for ASP.NET Core. Project moved to https://github.com/aspnet/AspNetCore
Apache License 2.0
169 stars 84 forks source link

Remote IP on Reverse Proxy scenarios #256

Closed Tratcher closed 7 years ago

Tratcher commented 7 years ago

From @luisgizirian on July 29, 2017 17:2

Why is that Context.Connection.RemoteIpAddress supplies the Proxy's address in scenarios where ForwardedHeadersOptions were applied (either automatically by UseIISIntegration or manually), but Context.Request.Headers["X-Forwarded-For"][0] gives me the right address?

If I don't know beforehand the ForwardedHeadersOptions.KnownProxies, Is it possible to use the first X-Forwarded-For address to pre-fill Context.Connection.RemoteIpAddress? (ForwardedHeadersOptions.ForwardLimit = 1 its default. And RequireHeaderSymmetry = false ) so I always get the Client/Browser IP address no matter what?

Tested scenarios (all based on ASPNET Core 2 preview 2):

An app comparing and writing back both values, following this document (https://docs.microsoft.com/en-us/aspnet/core/publishing/linuxproduction) is all you need.

Quick short-circuit: Configure in Startup.cs:

app.Run(async (context) => await context.Response.WriteAsync("RemoteIPAddress property: " + context.Connection.RemoteIpAddress.ToString() + "\nX-Forwarded-For [0] Header: " + context.Request.Headers["X-Forwarded-For"][0] ));
                })

Please advice.

(my apologies if this is not the right place to post this question)

Copied from original issue: aspnet/KestrelHttpServer#1974

Tratcher commented 7 years ago

Can you share all of the headers in your scenario? There are known cases where ForwardedHeaders fails to process some kinds of input.

luisgizirian commented 7 years ago

So sorry. Moved on and took those environments down (damn cost savings!). I'm using the x-forwarded-for header right away cause I needed to solve a production scenario. On Wed, Aug 2, 2017 at 3:17 PM Chris Ross notifications@github.com wrote:

Can you share all of the headers in your scenario? There are known cases where ForwardedHeaders fails to process some kinds of input.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/aspnet/BasicMiddleware/issues/256#issuecomment-319755110, or mute the thread https://github.com/notifications/unsubscribe-auth/AAkinZepca5OZ0_5HYeuxfqcqB2x0Dkyks5sUL1CgaJpZM4OnbmA .

muratg commented 7 years ago

Not enough information on this bug so closing it. If anyone has similar issues, please file another one with details.

darting commented 5 years ago

Hi there .. I have same issue when the app running on docker container ..

image

but if its not in container, its working

image

Tratcher commented 5 years ago

Comments on closed issues are not tracked, please open a new issue with the details for your scenario.

The results in your last screen shot look correct, what problem are you having specifically?