User IP Address , Using Proxy or Not
Please see the below codes :
private string GetUserIPAddress()
{
string User_IPAddress = string.Empty;
string User_IPAddressRange = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (string.IsNullOrEmpty(User_IPAddressRange))//without Proxy detection
{
User_IPAddress = Request.ServerVariables["REMOTE_ADDR"];
//or
//Client_IPAddress = Request.UserHostAddress;
//or
//User_IPAddress = Request.ServerVariables["REMOTE_HOST"];
}
else////with Proxy detection
{
string[] splitter = { "," };
string[] IP_Array = User_IPAddressRange.Split(splitter,
开发者_Python百科 System.StringSplitOptions.None);
int LatestItem = IP_Array.Length - 1;
User_IPAddress = IP_Array[LatestItem - 1];
//User_IPAddress = IP_Array[0];
}
return User_IPAddress;
}
In the case of :
1-User_IPAddress = Request.ServerVariables["REMOTE_ADDR"];
Client_IPAddress = Request.UserHostAddress;
and
User_IPAddress = Request.ServerVariables["REMOTE_HOST"];
is the lower or middle line an alternate for the other lines?
Would you please give us some explain about these lines?
What are the differences ?
2-
User_IPAddress = IP_Array[LatestItem - 1];
and
User_IPAddress = IP_Array[0];
Which line should I use?
Would you please give explain about these lines?I Don't know the class but chances are Request.UserHostAddress is an alias for Request.ServerVariables["REMOTE_ADDR"]. Also REMOTE_HOST would be the hostname but in most cases will just be the ipaddress.
Format of X-Forwarded-For is client1, proxy1, proxy2. So you want the second one. User_IPAddress = IP_Array[0];
Just remember "Since it is easy to forge an X-Forwarded-For field the given information should be used with care."
精彩评论