If you're using a reverse proxy, a CDN such as Cloudflare, a third-party Wide Application Firewall, or any other intermediate caching and/or forwarding service to serve your website over the internet, you might want to ensure that the underlying web server (typically called "origin") is only accessible to the IP addresses of such service (the "edge"). Doing that is very important from an IT security perspective because it will prevent malicious attackers (assuming they manage to retrieve the origin server's IP address) to bypass the edge proxy and directly connect to the origin server.
To comply with that using IIS, we're published several posts explaining how to set up the IP Addresses restriction built-in IIS feature using either the IIS Manager GUI or the web.config file. Such an approach is great most of the time, but what if you want to achieve the same outcome programmatically? More precisely, is there a way to check if a given IP address - let's say, the caller's IP address - is part of a given Subnet Mask in C#?
IsInSubnetMask helper method
As a matter of fact, the answer is YES! Here's a neat method that can be used to achieve such a result, compatible with IPv4 and IPv6 as long as we use CIDR notation (IPAddress/PrefixLength - example: 184.108.40.206/24).
/// Returns TRUE if the given IP address is contained in the given subnetmask, FALSE otherwise.
/// - IsInSubnet("192.168.5.1", "192.168.5.85/24") -> TRUE
/// - IsInSubnet("192.168.5.1", "192.168.5.85/32") -> FALSE
/// ref.: https://stackoverflow.com/a/56461160
/// <param name="address">The IP Address to check</param>
/// <param name="subnetMask">The SubnetMask</param>
/// <exception cref="NotSupportedException"></exception>
/// <exception cref="ArgumentException"></exception>
public static bool IsInSubnetMask(string ipAddress, string subnetMask)
var address = IPAddress.Parse(ipAddress);
var slashIdx = subnetMask.IndexOf("/");
if (slashIdx == -1)
// We only handle netmasks in format "IP/PrefixLength".
throw new NotSupportedException("Only SubNetMasks with a given prefix length are supported.");
// First parse the address of the netmask before the prefix length.
var maskAddress = IPAddress.Parse(subnetMask.Substring(0, slashIdx));
if (maskAddress.AddressFamily != address.AddressFamily)
// We got something like an IPV4-Address for an IPv6-Mask. This is not valid.
// Now find out how long the prefix is.
int maskLength = int.Parse(subnetMask.Substring(slashIdx + 1));
if (maskLength == 0)
if (maskLength < 0)
throw new NotSupportedException("A Subnetmask should not be less than 0.");
if (maskAddress.AddressFamily == AddressFamily.InterNetwork)
var maskAddressBits = BitConverter.ToUInt32(maskAddress.GetAddressBytes().Reverse().ToArray(), 0);
var ipAddressBits = BitConverter.ToUInt32(address.GetAddressBytes().Reverse().ToArray(), 0);
uint mask = uint.MaxValue << (32 - maskLength);
// Bitwise AND mask and MaskAddress, this should be the same as mask and IpAddress
// as the end of the mask is 0000 which leads to both addresses to end with 0000
// and to start with the prefix.
return (maskAddressBits & mask) == (ipAddressBits & mask);
if (maskAddress.AddressFamily == AddressFamily.InterNetworkV6)
// Convert the mask address to a BitArray. Reverse the BitArray to compare the bits of each byte in the right order.
var maskAddressBits = new BitArray(maskAddress.GetAddressBytes().Reverse().ToArray());
// And convert the IpAddress to a BitArray. Reverse the BitArray to compare the bits of each byte in the right order.
var ipAddressBits = new BitArray(address.GetAddressBytes().Reverse().ToArray());
var ipAddressLength = ipAddressBits.Length;
if (maskAddressBits.Length != ipAddressBits.Length)
throw new ArgumentException("Length of IP Address and Subnet Mask do not match.");
// Compare the prefix bits.
for (var i = ipAddressLength - 1; i >= ipAddressLength - maskLength; i--)
if (ipAddressBits[i] != maskAddressBits[i])
The above method is strongly based on this StackOverflow answer by Christoph Sonntag: from what we have seen, it works perfectly! If you take a look at the original answer, you'll also find some useful XUnit tests.
That's it: we hope that the above helper method will help other C# and ASP.NET Core developers to check if a specific IP address is part of a given Subnet Mask.