C#soapheader ip 过滤

 

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Collections;
using System.Text.RegularExpressions;
using System.Xml;
using Core;

namespace GreenWebService
{
    #region MyRegion
    

    /// <summary>
    /// 此类为单例模式,实现ip认证,调用方法如果下
    /// AuthIP.getInstance(AppDomain.CurrentDomain.BaseDirectory + "App_Data/ipconfig.xml").AuIp("192.168.0.1")
    /// 其中AppDomain.CurrentDomain.BaseDirectory + "App_Data/ipconfig.xml"为配置文件xml的路径
    /// 192.168.0.1为需要认证的IP地址
    /// </summary>
    public class AuthIP
    {

        public const String REGEX_IP = @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$"; //验证ip地址正则表达式

        //验证ip段地址正则表达式
        public const String REGEX_AREA_IP = @"((25[0-5]|2[0-4]\d|1\d{2}|[1-9]\d|\d)\.){3}(25[0-5]|2[0-4]\d|1\d{2}|[1-9]\d|\d)-((25[0-5]|2[0-4]\d|1\d{2}|[1-9]\d|\d)\.){3}(25[0-5]|2[0-4]\d|1\d{2}|[1-9]\d|\d)";

        private ArrayList auths = new ArrayList();

        private static AuthIP authInstance;

        private AuthIP() { }

        public static AuthIP getInstance(String filepath)
        {
            if (null == authInstance)
            {
                authInstance = new AuthIP();
                authInstance.ParseXml(filepath);
            }
            return authInstance;
        }

        public bool AuthIp(String ip)
        {
            if(null == ip || !IsIp(ip))
            {
                throw new Exception("参数是空的或者参数不是ip");
            }

            foreach (IAuthIP iau in auths)
            {
                if (iau.Auth(ip))
                {
                    return true;
                }
            }

            return false;
        }

        /// <summary>
        /// 读取配置文件里的配置信息
        /// </summary>
        /// <param name="filepath"></param>
        private void ParseXml(String filepath)
        {
            XmlNodeList nlist = XmlHelper.GetAreaString(filepath);
            if (nlist.Count < 1) return ;
            IAuthIP auth = null;
            foreach (XmlNode xn in nlist)
            {
                if (xn.LocalName == "ip" || xn.LocalName == "ips")
                {
                    auth = new ArrayIAuthIP(xn.InnerText);
                    auths.Add(auth);
                }
                else if (xn.LocalName == "areaip")
                {
                    auth = new AreaIAuthIP(xn.InnerText);
                    auths.Add(auth);
                }
            }
        }

        /// <summary>
        /// 认证接口,如果要添加新的认证规则 则必须要实现此接口
        /// </summary>
        public interface IAuthIP {

            bool Auth(String ip);

        }

        private class ArrayIAuthIP : IAuthIP {

            private ArrayList authIps;

            public ArrayIAuthIP (String tag)
	        {
                authIps = new ArrayList();
                Inflater(tag);
	        }

            /// <summary>
            /// 转换给定ip规则字符串,如192.168.0.1,192.168.0.1
            /// </summary>
            /// <param name="tag"></param>
            public void Inflater(String tag)
            {
                if (null == tag || tag == "") return;

                String[] strs = tag.Split(',');
                foreach (String str in strs)
                {
                    if (IsIp(str))
                    {
                        authIps.Add(str);
                    }
                    else
                    {
                        throw new Exception("请检查配置文件<ip>或者<ips>标签");
                    }
                }

            }

            public bool Auth(String ip)
            {
                foreach (String authIp in authIps)
                {
                    if (authIp == ip) 
                    {
                        return true;
                    }
                }

                return false;
            }
        }


        private class AreaIAuthIP : IAuthIP
        {
            private String areaStr;

            public AreaIAuthIP(String tag)
            {
                Inflater(tag);
            }
            
            private void Inflater(string tag)
            {
                if (null == tag || tag == "") return;

                if (IsAreaIp(tag))
                {
                    areaStr = tag;
                }
                else
                {
                    throw new Exception("请检查配置文件areaip标签");
                }
            }

            public bool Auth(String authIp)
            {
                if(null == areaStr)
                {
                    return false;
                }

                String[] areaIps = areaStr.Split('-');
                String startIp = areaIps[0];
                String endIp = areaIps[1];
                String[] startIpArray = startIp.Split('.');
                String[] endIpArray = endIp.Split('.');
                String[] authIpArray = authIp.Split('.');

                long startIpLong = 0L;
                long endIpLong = 0L;
                long authIpLong = 0L;

                for (int i = 0; i < 4; i++ )
                {
                    startIpLong = startIpLong << 8 | Convert.ToUInt32(startIpArray[i]);
                    endIpLong = endIpLong << 8 | Convert.ToUInt32(endIpArray[i]);
                    authIpLong = authIpLong << 8 | Convert.ToUInt32(authIpArray[i]);
                }

                if (startIpLong > endIpLong)
                {
                    long temp = 0L;
                    temp = startIpLong;
                    startIpLong = endIpLong;
                    endIpLong = temp;
                }
                return startIpLong <= authIpLong && endIpLong >= authIpLong ;
            }

        }
        
        /// <summary>
        ///  检查给定的字符串是否是ip类型
        /// </summary>
        /// <param name="ip"></param>
        /// <returns></returns>
        public static bool IsIp(string ip){

            return Regex.IsMatch(ip, REGEX_IP);

        }

        /// <summary>
        /// 检查给定的字符串是否是ip段类型
        /// </summary>
        /// <param name="areaIp"></param>
        /// <returns></returns>
        public static bool IsAreaIp(String areaIp)
        {
            return Regex.IsMatch(areaIp , REGEX_AREA_IP);
        }

    }
#endregion
}

 配置文件

 

<?xml version="1.0" encoding="utf-8"?>
<ipallows>
	<ip>192.168.0.1</ip>
	<ips>127.0.0.1,127.0.0.2</ips>
	<areaip>61.234.145.102-61.235.145.102</areaip>
</ipallows>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值