// Define the allowed IP address. The format is as follows:
Static String [] Allowipranges = { " 10.0.0.0-10.20.255.255 " , " 172.16.0.0-172.31.255.255 " , " 192.168.0.0-192.168.255.255 " }; // Main function, call the judgment Interface
Static Void Main ( String [] ARGs) {// determines whether the IP address 192.168.100.0 is in the specified IP range.
// For this range, if the IP address is converted to the long type, the IP address 192.167.0.0 will be in the range 10.0.0.0-10.255.255.255, but this is actually incorrect. You also want to switch the IP address to long at a high point.
Console. writeline (theipisrange ( " 192.169.100.0 " , Allowipranges ));
Console. writeline ( " Done " ); Console. Read () ;}// the interface function parameters are the IP address you want to determine and the IP range you allow. // (already overloaded)
// (Multiple arrays can be specified at the same time)
Static Bool Theipisrange ( String IP, Params String [] Ranges ){ Bool Tmpres = False ; Foreach ( VaR Item In Ranges ){ If (Theipisrange (IP, item) {tmpres = True ; Break ;}} Return Tmpres ;} /// <Summary> /// Determines whether the specified IP address is within the specified IP address range. Only one range can be specified here.
/// </Summary> /// <Param name = "ip"> </param> /// <Param name = "ranges"> </param> /// <Returns> </returns> Static Bool Theipisrange ( String IP, String Ranges ){ Bool Result = False ; Int Count; String Start_ip, end_ip; // check whether the specified IP range is valid
Tryparseranges (ranges, Out Count, Out Start_ip, Out End_ip ); // Check whether the IP Range format is valid If (IP = " : 1 " ) IP = " 127.0.0.1 " ; Try {IPaddress. parse (IP); // determines whether the specified IP address is valid.
} Catch (Exception ){ Throw New Applicationexception ( " The IP address to be detected is invalid. " );} If (COUNT = 1 & Amp; IP = start_ip) Result = True ; // If the specified IP address range is an IP address, directly match to see if it is equal
Else If (COUNT = 2 ) // If the specified IP range is a starting IP Range
{ Byte [] Start_ip_array = Get4byte (start_ip); // converts the vertex decimal to a byte array of four elements.
Byte [] End_ip_array = Get4byte (end_ip ); Byte [] Ip_array =Get4byte (IP ); Bool Tmpres = True ; For ( Int I = 0 ; I < 4 ; I ++ ) {// Compare the values of the corresponding positions from left to right. Once detecting that the IP address is not in the specified range, the loop is terminated.
If (Ip_array [I]> end_ip_array [I] | ip_array [I] < Start_ip_array [I]) {tmpres =False ; Break ;} Result = Tmpres ;} Return Result;} // try to resolve the IP Range and obtain the starting IP (inclusive) of the closed range)
Private Static Void Tryparseranges ( String Ranges, Out Int Count, Out String Start_ip, Out String End_ip ){ String [] _ R = ranges. Split ( ' - ' ); If (! (_ R. Count () = 2 | _ R. Count () = 1 )) Throw New Applicationexception ( " The specified IP Range format is incorrect. You can specify an IP address. If it is a range, use "-" to separate it. " ); Count = _ R. Count (); start_ip = _ R [ 0 ]; End_ip = "" ; Try {IPaddress. parse (_ R [ 0 ]);} Catch (Exception ){ Throw New Applicationexception ( " The IP address is invalid. " );} If (_ R. Count () = 2 ) {End_ip = _ R [ 1 ]; Try {IPaddress. parse (_ R [ 1 ]);} Catch (Exception ){ Throw New Applicationexception ( " The IP address is invalid. " );}}} /// <Summary> /// Convert four IP group values to byte type /// </Summary> /// <Param name = "ip"> </param> /// <Returns> </returns> Static Byte [] Get4byte ( String IP ){ String [] _ I = IP. Split ( ' . ' ); List < Byte > Res = New List < Byte > (); Foreach ( VaR Item In _ I) {res. Add (convert. tobyte (item ));} Return Res. toarray ();}
If there are any deficiencies or better methods, please exchange ideas.
Just after the release, a netizen told me that a simpler method is to convert the IP address to the long type.
IPaddress I = IPaddress. parse ("255.255.255.255");LongL =I. Address; console. writeline (l); console. writeline (Long. Maxvalue );
However, after testing, we found that the method for converting to the long type still has the following problems:
For example, 192.168.0.0 ~ 192.168.255.255 and 10.0.0.0 ~ 10.20.255.255 is the allowed IP address.
If 192.165.0.0 is not in the preceding range, it is not in the following range.
However, the long method will detect the following range.
Use the above method to accurately detect