How to get Client or visitor IP Address:: Despite multiple tries Didn't get solution: Below what I tried:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string ipAddress33 = Request.UserHostAddress.ToString();
string strHostName = System.Net.Dns.GetHostName();
string clientIPAddress22 = System.Net.Dns.GetHostAddresses(strHostName).GetValue(1).ToString();
Response.Write("System.Net.Dns.GetHostAddresses(strHostName).GetValue(1).ToString(); : " + clientIPAddress22 + "<br />");
string ipaddress;
string IP = Request.UserHostAddress;
string clientIPAddress = this.Page.Request.ServerVariables["REMOTE_ADDR"];
string IP2 = Environment.GetEnvironmentVariable("CLIENTNAME");
ipaddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (ipaddress == "" || ipaddress == null)
ipaddress = Request.ServerVariables["REMOTE_ADDR"];
Response.Write("Request.ServerVariables['HTTP_X_FORWARDED_FOR'] : " + ipaddress + "<br />");
Response.Write("Request.UserHostAddress.ToString() : " + ipAddress33 + "<br />");
string stringIpAddress;
stringIpAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (stringIpAddress == null) //may be the HTTP_X_FORWARDED_FOR is null
{
stringIpAddress = Request.ServerVariables["REMOTE_ADDR"];//we can use REMOTE_ADDR
}
Response.Write("Request.ServerVariables['REMOTE_ADDR'] : " + stringIpAddress + "<br />");
//Get the Host Name
string stringHostName = Dns.GetHostName();
//Get The Ip Host Entry
IPHostEntry ipHostEntries = Dns.GetHostEntry(stringHostName);
//Get The Ip Address From The Ip Host Entry Address List
IPAddress[] arrIpAddress = ipHostEntries.AddressList;
Response.Write("Dns.GetHostName(): " + arrIpAddress[arrIpAddress.Length - 1].ToString());
}
}
This is the results I got:
System.Net.Dns.GetHostAddresses(strHostName).GetValue(1).ToString(); : 190.80.90.75
Request.ServerVariables['HTTP_X_FORWARDED_FOR'] : 190.80.90.225
Request.UserHostAddress.ToString() : 190.80.90.225
Request.ServerVariables['REMOTE_ADDR'] : 190.80.90.225
Dns.GetHostName(): 190.80.90.75
Which is not the correct IP address of the visitor.