Please let me know how to get the client IP address,
I have tried all of the below things , but I am getting the same output: 127.0.0.1
string strClientIP;
strClientIP = Request.UserHostAddress.ToString();
string strHostName = System.Net.Dns.GetHostName();
string clientIPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString();
string ipaddress = string.Empty ;
ipaddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (ipaddress == "" || ipaddress == null)
ipaddress = Request.ServerVariables["REMOTE_ADDR"];
How can I get the correct IP?
You are on the right track with
REMOTE_ADDR, but it might not work if you are accessing the site locally, it will show local host.REMOTE_ADDRis the header that contains the clients ip address you should check that first.You should also check to for the
HTTP_X_FORWARDEDheader in case you’re visitor is going through a proxy. Be aware thatHTTP_X_FORWARDEDis an array that can contain multiple comma separated values depending on the number of proxies.Here is a small c# snippet that shows determining the client’s ip: