一个验证IP范围内权限的C#方法,支持 ? * -
来源:互联网 发布:淘宝迅雷会员1分钱 编辑:程序博客网 时间:2024/06/14 15:43
- protected void Page_Load(object sender, EventArgs e)
- {
- Response.Write(IsAllowIP("192.168.1.231-236:yes;192.168.1.238:no", "192.168.1.235"));
- Response.Write(IsAllowIP("192.168-169.1.23*:no", "192.168.1.235"));
- Response.Write(IsAllowIP("1*.168.1.23?:yes", "192.168.1.235"));
- Response.Write(IsAllowIP("1?2.16*.1.231-236:no", "192.168.1.235"));
- }
- #region 验证在IP范围内是否允许
- public static bool IsAllowIP(string rule, string ip)
- {
-
- string ipRegexString = @"^((2[0-4]/d|25[0-5]|[01]?/d/d?)/.){3}(2[0-4]/d|25[0-5]|[01]?/d/d?)$";
-
- if (!Regex.IsMatch(ip, ipRegexString))
- {
- throw new Exception("参数ip错误:错误的IP地址" + ip);
- }
- else
- {
-
- string[] ruleArray = rule.Split(new char[] { ';' });
- string[] ipdata = ip.Split(new char[] { '.' });
- bool retValue = false;
-
- foreach (string s in ruleArray)
- {
- bool IsFind = false;
- string[] data = s.Split(new char[] { ':' });
-
- if (data.Length != 2) { throw new Exception("请用:分开 如:192.168.1.1:yes"); }
- string ruleIp = data[0];
- retValue = data[1].ToString().ToLower() == "yes" ? true : false;
- string[] ruleIpArray = ruleIp.Split(new char[] { '.' });
- if (ruleIpArray.Length != 4) { throw new Exception("IP部分错误!"); }
- #region
- for (int i = 0; i < 4; i++)
- {
- bool AA = ruleIpArray[i].Contains("*");
- bool BB = ruleIpArray[i].Contains("-");
- bool CC = ruleIpArray[i].Contains("?");
- if ((AA && BB) || (AA && CC) || (BB && CC) || (AA && BB && CC))
- {
- throw new Exception("这样的格式是错误的,192.168.15-20*,*与-不能包含在同一个部分! ");
- }
- else if (!AA && !BB && !CC)
- {
- if (!Regex.IsMatch(ruleIpArray[i], @"^2[0-4]/d|25[0-5]|[01]?/d/d?$"))
- {
- throw new Exception("IP段错误应该在1~255之间:" + ruleIpArray[i]);
- }
- else
- {
- #region 这里判断 111111111111
- if (ruleIpArray[i] == ipdata[i])
- {
- IsFind = true;
- }
- else
- {
- IsFind = false;
- break;
- }
- #endregion
- }
- }
- else if (AA && !BB && !CC)
- {
- if (ruleIpArray[i] != "*")
- {
- if (ruleIpArray[i].StartsWith("*") || !ruleIpArray[i].EndsWith("*") || ruleIpArray[i].Contains("**"))
- {
- throw new Exception("IP中的*部分:不能以*开头,不能有两个**,只能以*结尾");
- }
- }
- else
- {
- #region 这里判断22222222222222
- if (ipdata[i].StartsWith(ruleIpArray[i].Replace("*", "")))
- {
- IsFind = true;
- }
- else
- {
- IsFind = false;
- break;
- }
- #endregion
- }
- }
- else if (BB && !AA && !CC)
- {
- string[] temp = ruleIpArray[i].Split(new char[] { '-' });
- if (temp.Length != 2)
- {
- throw new Exception("IP段错误, 如:23-50,在1~255之间");
- }
- else
- {
- if (Convert.ToInt32(temp[0]) < 1 || Convert.ToInt32(temp[1]) > 255)
- {
- throw new Exception("IP段错误, 如:23-50,在1~255之间");
- }
- else
- {
- #region 这里判断33333333333333333
- string[] Num = ruleIpArray[i].Split(new char[] { '-' });
- int p = int.Parse(ipdata[i]);
- if (p >= int.Parse(Num[0]) && p <= int.Parse(Num[1]))
- {
- IsFind = true;
- }
- else
- {
- IsFind = false;
- break;
- }
- #endregion
- }
- }
- }
- else if (CC && !AA & !BB)
- {
-
- string temp = ruleIpArray[i].Replace("?", "");
- Regex re = new Regex(@"^/d/d?$");
- if (!re.IsMatch(temp) || temp.Length > 2)
- {
- throw new Exception("IP段错误:" + ruleIpArray[i]);
- }
- else
- {
- #region 这里判断4444444444444
- if (ruleIpArray[i].Length != ipdata[i].Length)
- {
- IsFind = false;
- break;
- }
- else
- {
- string tempRegstring = "^" + ruleIpArray[i].Replace("?", @"/d") + "$";
- Regex tempRe = new Regex(tempRegstring);
- if (tempRe.IsMatch(ipdata[i]))
- {
- IsFind = true;
- }
- else
- {
- IsFind = false;
- break;
- }
- }
- #endregion
- }
- }
- else
- {
- IsFind = false;
- break;
- }
- }
- #endregion
- if (IsFind)
- {
- return retValue;
- }
- }
- return false;
- }
- }
- #endregion