Introduction:
In this article I will explain how to find system or client machine IP address from which user visited the website in asp.net behind proxy in c#.
protected void Page_Load(object sender, EventArgs e) //or
protected void Page_Load(object sender, EventArgs e)
IPHostEntry GetIPHost = Dns.GetHostEntry(Request.UserHostName);
Response.Write(GetIPHost.HostName);
}
If you observe above code when users behind any proxies or routers the REMOTE_ADDR/Request.UserHostName returns the IP Address of the router and not the client user’s machine. Hence first we need to check HTTP_X_FORWARDED_FOR/Request.UserHostName, 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.
In this article I will explain how to find system or client machine IP address from which user visited the website in asp.net behind proxy in c#.
Description:
Now I will explain how to get system or client IP address of machine from which the user visited the website in asp.net in c#. To implement this one write the code as shown below
Now I will explain how to get system or client IP address of machine from which the user visited the website in asp.net in c#. To implement this one write the code as shown below
{
string IPAdd = string.Empty;
IPAdd = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (string.IsNullOrEmpty(IPAdd))
IPAdd = Request.ServerVariables["REMOTE_ADDR"];
lblIP.Text = IPAdd;
}
{
IPAddress myIP = IPAddress.Parse(Request.UserHostName);IPHostEntry GetIPHost = Dns.GetHostEntry(Request.UserHostName);
Response.Write(GetIPHost.HostName);
}
If you observe above code when users behind any proxies or routers the REMOTE_ADDR/Request.UserHostName returns the IP Address of the router and not the client user’s machine. Hence first we need to check HTTP_X_FORWARDED_FOR/Request.UserHostName, 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.