cidr range validator C#
using System.Net; //Namespace
//This will retrun True if the input IP is in cidr range
private bool IsInCIDRRange(string ipAddress, string cidr)
{
string[] splitDetails = cidr.Split('/');
int baseAddress = BitConverter.ToInt32(IPAddress.Parse(splitDetails[0]).GetAddressBytes(), 0);
int address = BitConverter.ToInt32(IPAddress.Parse(ipAddress).GetAddressBytes(), 0);
int mask = IPAddress.HostToNetworkOrder(-1 << (32 - int.Parse(splitDetails[1])));
return ((baseAddress & mask) == (address & mask));
}
0 comments: