Click here to Skip to main content
15,869,940 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my project,if client sent contact details, admin need to get client Computer IP address. so i used the following code to get Client IP address.But i got wrong IP address.How can i get Client IP address after hosted to live server.
C#
string clientIp = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (!string.IsNullOrEmpty(clientIp))
{
    string[] forwardedIps = clientIp.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
    clientIp = forwardedIps[forwardedIps.Length - 1];
}
else
{
    clientIp = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
}
Response.Write(clientIp);
Posted
Comments
Thanks7872 3-Dec-13 2:40am    
What was the IP you got that is wrong? Is it 127.0.0.1?
Amir Mahfoozi 3-Dec-13 2:47am    
Pay attention that testing this code in a local network will give you a different result rather than testing it on a live server.

This single line of code will give you what are you looking for
string IP= Server.HtmlEncode(Request.UserHostAddress);

When you run this on localhost,it will always give you 127.0.0.1
 
Share this answer
 
Hi...
See this link, may its help full to u.
http://msdn.microsoft.com/en-us/library/system.web.httprequest.userhostaddress.aspx[^]
Thank u.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900