Created
April 21, 2020 07:49
-
-
Save sandcastle/b30679cf18dd5b8a1200b7614e0a58ec to your computer and use it in GitHub Desktop.
Check if loop back is private network or a loopback address...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
Source: