屏蔽ip网段

配置文件

 

      <add key="BeginIPInner" value="10.32.0.0"/>
      <add key="EndIPInner" value="10.32.254.254"/>
      <add key="BeginIPInner2" value="192.168.0.0"/>
      <add key="EndIPInner2" value="192.168.254.254"/>
      <add key="BeginIPOuter" value="60.247.44.96"/>
      <add key="EndIPOuter" value="60.247.44.128"/>

 

 

.cs代码

        public static Int32 CreateUnionOrder(OrderInfo info)
        {
 

        bool bIP = true;
            string userip = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];//代理服务器的随机IP地址
            var bIPinner = long.Parse(IPToInt(ConfigurationManager.AppSettings["BeginIPInner"]).ToString());
            var eIPinner = long.Parse(IPToInt(ConfigurationManager.AppSettings["EndIPInner"]).ToString());
            var bIPinner2 = long.Parse(IPToInt(ConfigurationManager.AppSettings["BeginIPInner2"]).ToString());
            var eIPinner2 = long.Parse(IPToInt(ConfigurationManager.AppSettings["EndIPInner2"]).ToString());
            var bIPouter = long.Parse(IPToInt(ConfigurationManager.AppSettings["BeginIPOuter"]).ToString());
            var eIPouter = long.Parse(IPToInt(ConfigurationManager.AppSettings["EndIPOuter"]).ToString());
            long lInt = 0;

            if (userip == "" || userip == null)
            {
                userip = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];//客户端ip
                lInt = long.Parse(IPToInt(userip).ToString());

      //如果ip段 在10.32.0.0 到10.32.254.254中间,或者在192.168.0.0到192.168.254.254或者在60.247.44.96到60.247.44.128之间,返回false,不执行业务逻辑,达到屏蔽ip段的目的

                if ((lInt >= bIPinner && lInt <= eIPinner) || (lInt >= bIPinner2 && lInt <= eIPinner2) || (lInt >= bIPouter && lInt <= eIPouter))
                {
                    bIP = false;
                }
            }
            if (bIP)
            {

       //要执行的业务逻辑

    }

   }

 

        public static uint IPToInt(string ipAddress)//把ip转成数字,其实就是把八进制数转换成十进制
        {
            string disjunctiveStr = ".,: ";
            char[] delimiter = disjunctiveStr.ToCharArray();
            string[] startIP = null;
            for (int i = 1; i <= 5; i++)
            {
                startIP = ipAddress.Split(delimiter, i);
            }
            string a1 = startIP[0].ToString();
            string a2 = startIP[1].ToString();
            string a3 = startIP[2].ToString();
            string a4 = startIP[3].ToString();
            uint U1 = uint.Parse(a1);
            uint U2 = uint.Parse(a2);
            uint U3 = uint.Parse(a3);
            uint U4 = uint.Parse(a4);

            uint U = U1 << 24;
            U += U2 << 16;
            U += U3 << 8;
            U += U4;
            return U;
        }

 

        public static string IntToIP(uint ipAddress)//数字转换成ip地址
        {
            long ui1 = ipAddress & 0xFF000000;
            ui1 = ui1 >> 24;
            long ui2 = ipAddress & 0x00FF0000;
            ui2 = ui2 >> 16;
            long ui3 = ipAddress & 0x0000FF00;
            ui3 = ui3 >> 8;
            long ui4 = ipAddress & 0x000000FF;
            string IPstr = "";
            IPstr = System.Convert.ToString(ui1) + "."
                + System.Convert.ToString(ui2) + "."
                + System.Convert.ToString(ui3)
                + "." + System.Convert.ToString(ui4);
            return IPstr;
        }

 

 

 

 

 

posted @ 2010-12-15 10:29  艾伦  阅读(830)  评论(0编辑  收藏  举报