User IP Address , Using Proxy or Not

不想你离开。 提交于 2019-12-07 19:41:21

问题


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,
                                                          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"];
and
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?


回答1:


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."



来源:https://stackoverflow.com/questions/6681024/user-ip-address-using-proxy-or-not

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!