Get Client IP in your site
It’s pretty common the need for a web application to be able to detect the I.P. address of a client. The I.P. address could be used either for statistic or authentication purposes.
<pre lang="cs">public object ClientIpAddress(HttpRequest myRequest)
{
string myIP = "";
if ((string.IsNullOrEmpty(myIP)))
myIP = myRequest.ServerVariables("HTTP_CLIENT_IP");
if ((string.IsNullOrEmpty(myIP)))
myIP = myRequest.ServerVariables("HTTP_X_FORWARDED_FOR");
if ((string.IsNullOrEmpty(myIP)))
myIP = myRequest.ServerVariables("HTTP_X_FORWARDED");
if ((string.IsNullOrEmpty(myIP)))
myIP = myRequest.ServerVariables("HTTP_X_CLUSTER_CLIENT_IP");
if ((string.IsNullOrEmpty(myIP)))
myIP = myRequest.ServerVariables("HTTP_FORWARDED_FOR");
if ((string.IsNullOrEmpty(myIP)))
myIP = myRequest.ServerVariables("HTTP_FORWARDED");
if ((string.IsNullOrEmpty(myIP)))
myIP = myRequest.ServerVariables("REMOTE_ADDR");
return myIP;
}
public string Host2Ip(string HostName)
{
string functionReturnValue = null;
try {
System.Net.IPHostEntry myIPs = null;
myIPs = System.Net.Dns.GetHostEntry(HostName);
functionReturnValue = myIPs.AddressList[0].ToString();
} catch (Exception ex) {
}
return functionReturnValue;
}</pre>