I've read multiple questions (and answers) before I posted this, but I can't seem to get it somehow.. How do I calculate an IP range using REMOTE_ADDR? For example I've given exlusive access to the range: 197.168.178.1 - 197.168.178.10 When I access the site with 197.168.178.6 I want to get access. How do I manage that?
What I got so far is a List of the class IpRange - these IP addresses are specified in the database and can be altered to any range. The ID is used to identify which range is accessing the site.
public class IpRange
{
public int ID { get; set; }
public System.Net.IPAddress RangeStart { get; set; }
public System.Net.IPAddress RangeEnd { get; set; }
}
And I've tried to match the incoming IP address (Request.ServerVariables["REMOTE_ADDR"]
) to this range using the next method;
private static int MatchAddress(System.Net.IPAddress IP, List<IpRange> range)
{
int Value = -1;
foreach (IpRange item in range)
{
if (item.RangeStart.Address >= IP.Address && IP.Address <= item.RangeEnd.Address)
{
Value = item.ID;
break;
}
}
return Value;
}
The method Address is obsolete and most likely returns something completely different than I think it does; according the warning I should use IPAddress.Equals
, but that method can only compare two addresses to see if it's the same, I want to know if it's defined within the range.
What is a method to match this (or should I use another component to do this)?
P.S. My knowdledge of networking is well below par, I know that, so it might be a really dumb question, but I'm lost atm.