限制一个IP如果它是一个IP范围之间 [英] Restricting an IP if it is between an IP Range

查看:131
本文介绍了限制一个IP如果它是一个IP范围之间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,这是周五的下午,我有一个漫长的一周,以便将不胜感激一些帮助!
目前,我有IP范围列表,如下所示:

 列表< IPRange> ipRanges =新的List< IPRange>(); 

ipRanges.Add(新IPRange {从=145.36.0.0,要=145.36.255.255});
ipRanges.Add(新IPRange {从=194.183.227.184,要=194.183.227.191});
ipRanges.Add(新IPRange {从=193.131.192.0,要=193.131.223.255});



获得客户机的IP,如果这些组范围介于任意位置后,它们需要被重定向到其他地方。



例如,



如果有人访问该网站的IP 192.168.0.1 ,他们将被允许访问。
如果他们与 145.36.1.0 参观,他们不会因为它属于在该列表中的第一个区域之间不允许访问。



我可能分裂的时期每个IP,以及在哪里工作范围内开始改变,那么做一个比较,但是这将是在服务器上的沉重。



我知道IP的基本上只是十进制数,但我真的不知道如何工作的。



能欣赏到这之前?



干杯,肖恩。


有人进来< DIV CLASS =h2_lin>解决方案

将每个IP地址的进行编号,然后检查,如果用户的IP地址是这些数字之间。

 公共双Dot2LongIP(字符串DottedIP)
{
INT I;
的String [] arrDec;
双NUM = 0;
如果(DottedIP ==)
{
返回0;
}
,否则
{
arrDec = DottedIP.Split('。');
为(I = arrDec.Length - 1; I> = 0; I - )
{
+ NUM =((int.Parse(arrDec [I])%256) * Math.Pow(256,(3 - ⅰ)));
}
返回NUM;
}
}


Ok, it's friday afternoon, and i've had a long week so would appreciate some help! Currently, i have a list of IP ranges, as follows:

List<IPRange> ipRanges = new List<IPRange>();

ipRanges.Add(new IPRange { From = "145.36.0.0", To = "145.36.255.255" });
ipRanges.Add(new IPRange { From = "194.183.227.184", To = "194.183.227.191" });
ipRanges.Add(new IPRange { From = "193.131.192.0", To = "193.131.223.255" });

After getting the IP of the client, if it falls anywhere between these sets of ranges, they need to be redirected elsewhere.

For example,

If someone visited the site with the IP 192.168.0.1, they would be allowed access. If they visited with 145.36.1.0, they would not be allowed access because it falls between the first range in that list.

I could split each IP by the period, and work out where the range starts to change, then do a comparison, but that would be heavy on the server.

I know IP's are basically just decimal numbers, but am not really sure how that works.

Has anyone come across this before?

Cheers, Sean.

解决方案

Convert Each IP-address to number, and then check if the user ip address is between those numbers.

public double Dot2LongIP(string DottedIP)
{
    int i;
    string [] arrDec;
    double num = 0;
    if (DottedIP == "")
    {
       return 0;
    }
    else
    {
       arrDec = DottedIP.Split('.');
       for(i = arrDec.Length - 1; i >= 0 ; i --)
       {
          num += ((int.Parse(arrDec[i])%256) * Math.Pow(256 ,(3 - i )));
       }
       return num;
    }
}

这篇关于限制一个IP如果它是一个IP范围之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆