简体   繁体   中英

How can an ASP.NET Core MVC app get client IP address when server is behind AWS ELB?

I'm running an ASP.NET Core MVC app on AWS in an Elastic Beanstalk environment that includes ELB.

I use this code to get the IP address of the client:

HttpContext.Connection.RemoteIpAddress.ToString()

However, this returns the IP address of the load balancer, not the IP address of the client. I believe RemoteIPAddress returns the address stored in the X-Forwarded-For HTTP header, which in theory should be the client IP, but it isn't.

So I followed the instructions to enable Proxy Protocol for ELB based on these instructions .

But still no luck. The RemoteIPAddress still returns the same ELB IP instead of the client IP.

Has anyone been able to get client IP when running an ASP.NET Core MVC app on AWS behind ELB? If so, how?

Any help would be greatly appreciated.

You can just look at the X-Forwarded-For header for the original IP address.

To access the Headers, look at HttpContext.Current.Request.Headers or HttpContext.Request.Headers .

I successfully listed the original client IP when running an ASP.NET Core MVC app on AWS behind ELB with that method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM