Tuesday, March 24, 2015

How to get IP Address of Visitors Machine || Client Machine in ASP.Net

Description: -This post explains you to know how to get the client IP Address from ASP.Net c#
C#
string ipaddress;
ipaddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (ipaddress == "" || ipaddress == null)
    ipaddress = Request.ServerVariables["REMOTE_ADDR"];

When users are behind any proxies or routers the REMOTE_ADDR returns the IP Address of the router and not the client user’s machine. Hence first we need to checkHTTP_X_FORWARDED_FOR, since when client user is behind a proxy server his machine’s IP Address the Proxy Server’s IP Address is appended to the client machine’s IP Address. If there are multiple proxy servers the IP Addresses of all of them are appended to the client machine IP Address.
Hence we need to first check HTTP_X_FORWARDED_FOR and then REMOTE_ADDR.

Your IP Address like …………
192.658.55.36
Note: - While executing this code it will show the wrong IP Address in local development in some case. After deploy in IIS it will show the correct address.


No comments:

Post a Comment