I want to create extension methods which is check sended IP address
Specify at least one IP address from which requests will be made. All requests from other IP addresses are denied. Access can be granted to:
A single IP address (e.g. 192.168.0.2)
Multiple IP addresses, separated by space (e.g. 192.168.0.2 10.0.0.2)
- A subnet in CIDR notation (e.g. 192.168.0.0/24)
Enter a list of IPs or at least one IP address (or an IP range) in the text fields in the corresponding sections.
below code which is use but it's not working
public static bool IsIpAddressAllowed(this string clientAddress, string allowedIpRange)
{
return IsInRange(clientAddress, allowedIpRange, allowedIpRange); ;
}
public static bool IsInRange(string startIpAddr, string endIpAddr, string address)
{
long ipStart = BitConverter.ToInt32(IPAddress.Parse(startIpAddr).GetAddressBytes().Reverse().ToArray(), 0);
long ipEnd = BitConverter.ToInt32(IPAddress.Parse(endIpAddr).GetAddressBytes().Reverse().ToArray(), 0);
long ip = BitConverter.ToInt32(IPAddress.Parse(address).GetAddressBytes().Reverse().ToArray(), 0);
return ip >= ipStart && ip <= ipEnd; //edited
}