Is there native .NET type for CIDR subnets?

No there is such native type in .NET, you will need to develop one your self.


For ASP.NET Core there is now IPNetwork class. Example usage below:

var addr = IPAddress.Parse("192.168.0.0");
var mask = 16;
var test = new IPNetwork(addr, mask).Contains(context.Connection.RemoteIpAddress);

You can use the code from GitHub to do just that:

https://github.com/lduchosal/ipnetwork

IPNetwork ipnetwork = IPNetwork.Parse("192.168.168.100/24");

Console.WriteLine("Network : {0}", ipnetwork.Network);
Console.WriteLine("Netmask : {0}", ipnetwork.Netmask);
Console.WriteLine("Broadcast : {0}", ipnetwork.Broadcast);
Console.WriteLine("FirstUsable : {0}", ipnetwork.FirstUsable);
Console.WriteLine("LastUsable : {0}", ipnetwork.LastUsable);
Console.WriteLine("Usable : {0}", ipnetwork.Usable);
Console.WriteLine("Cidr : {0}", ipnetwork.Cidr);

Output

Network : 192.168.168.0
Netmask : 255.255.255.0
Broadcast : 192.168.168.255
FirstUsable : 192.168.168.1
LastUsable : 192.168.168.254
Usable : 254
Cidr : 24