• 获取客服端IP的完整解决方案


    来自印度的MCT Maulik Patel提供了一种服务端的获取IP解决方案

    if(Context.Request.ServerVariables["HTTP_VIA"]!=null// using proxy

         ip=Context.Request.ServerVariables[
    "HTTP_X_FORWARDED_FOR"].ToString();  // Return real client IP.
    }
    else// not using proxy or can't get the Client IP

         ip=Context.Request.ServerVariables[
    "REMOTE_ADDR"].ToString(); //While it can't get the Client IP, it will return proxy IP.
    }

     PS

    1. 有些代理是不会发给我们真实IP地址的

    2. 有些客户端会因为“header_access deny”的安全设置而不发给我们IP

    但是由于第二个备注在我们获取real client IP.出错(所以对上面的解决方案做如下调整)

     现在做如下解决方案

    /// <summary>

                            /// 获取远程IP

                            /// </summary>

                            /// <returns>远程主机的IP地址</returns>

                            public static string GetCustomerIP()

                            {

                                        string CustomerIP="";

                if (HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null)// using proxy

                {

                    try

                    {

                        // Return real client IP.

                        CustomerIP = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();

                    }

                    catch

                    {

                        //While it can't get the Client IP, it will return proxy IP.

                        CustomerIP = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();

                    }

                                        }

                                        else

                                        {

                    //While it can't get the Client IP, it will return proxy IP.

                                                    CustomerIP=HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();

                                        }

                                        return CustomerIP;

            }

  • 相关阅读:
    ACM 人见人爱A^B
    ACM Max Factor
    ACM Primes
    ACM Least Common Multiple
    ACM 最小公倍数
    ACM Bone Collector
    ACM 悼念512汶川大地震遇难同胞——珍惜现在,感恩生活
    ACM Piggy Bank
    ACM 饭卡
    ACM Where is the Marble?
  • 原文地址:https://www.cnblogs.com/huangbaixun/p/1293114.html
Copyright © 2020-2023  润新知