Skip to content

Instantly share code, notes, and snippets.

@sandcastle
Created April 21, 2020 07:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sandcastle/b30679cf18dd5b8a1200b7614e0a58ec to your computer and use it in GitHub Desktop.
Save sandcastle/b30679cf18dd5b8a1200b7614e0a58ec to your computer and use it in GitHub Desktop.
Check if loop back is private network or a loopback address...
static bool IsLoopbackOrPrivate([NotNull] IPAddress clientIp)
{
// RFC for private networks:
// http://www.faqs.org/rfcs/rfc1918.html
byte[] bytes = clientIp.GetAddressBytes();
switch(bytes[0])
{
case 10:
return true;
case 172:
return bytes[1] < 32 && bytes[1] >= 16;
case 192:
return bytes[1] == 168;
default:
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment