Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does ServerVariable["REMOTE_ADDR"] returns the server IP?

Tags:

c#

.net

asp.net

I have the following code:

string ip = Request.ServerVariables["REMOTE_ADDR"];

Which, in the test environment does return the user IP addrress, but when we deploy the website to production, this variable has the IP of the server where the application is hosted. Any help?

like image 721
Felix Martinez Avatar asked Aug 02 '11 15:08

Felix Martinez


People also ask

What is server Remote_addr?

$_SERVER['REMOTE_ADDR'] gives the IP address from which the request was sent to the web server.

What is request ServerVariables Remote_addr?

ServerVariables("REMOTE_ADDR") is Always the Same. If your scripts use Request. ServerVariables("REMOTE_ADDR") to get the client's IP address, they will always show the same, internal IP address due to the load balancers used for hosting your site. You can get the client's remote IP using Request.

What is request ServerVariables Http_x_forwarded_for?

ServerVariables HTTP_X_FORWARDED_FOR is NULL when it is used to fetch the IP Address in ASP.Net using C# and VB.Net.


1 Answers

My guess is that there is a proxy in the middle. Use HTTP_X_FORWARDED_FOR first, and if that's null, then use REMOTE_ADDR

From the MSDN article:

Although retrieving just the REMOTE_ADDR server variable should be enough, I found resources online that suggested that code like this should also check the HTTP_X_FORWARDED_FOR variable; if the request comes through a proxy server that translates the address, it's this variable that contains the correct address. If you request a server variable that doesn't exist, the ServerVariables property returns an empty string. Therefore, even though this property doesn't appear in my tests, attempting to retrieve its value doesn't cause trouble.

UPDATE:

If it's a load balancer that you have have settings changed on, you should ask to see if they can have the origination IP passed through. I know this can be done with Microsoft's ISA server.

If that's not an option, there are these other server variables that you can try and see if they produce a result:

"HTTP_X_COMING_FROM"
"HTTP_X_FORWARDED_FOR"
"HTTP_X_FORWARDED"
"HTTP_X_REAL_IP"
"HTTP_VIA"
"HTTP_COMING_FROM"
"HTTP_FORWARDED_FOR"
"HTTP_FORWARDED"
"HTTP_FROM"
"HTTP_PROXY_CONNECTION"
"CLIENT_IP"
"FORWARDED"
like image 145
zeroef Avatar answered Sep 28 '22 08:09

zeroef