Created
December 4, 2016 01:16
-
-
Save lindexi/803867f7ddae1cf6e8f53b70629b0898 to your computer and use it in GitHub Desktop.
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
/// <summary> | |
/// 判断私有ip | |
/// </summary> | |
/// <param name="myIPAddress"></param> | |
/// <returns></returns> | |
public static bool IsPrivateIP(IPAddress myIPAddress) | |
{ | |
if (IPAddress.IsLoopback(myIPAddress)) | |
{ | |
return true; | |
} | |
if (myIPAddress.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) | |
{ | |
byte[] ipBytes = myIPAddress.GetAddressBytes(); | |
// 10.0.0.0/24 | |
if (ipBytes[0] == 10) | |
{ | |
return true; | |
} | |
// 172.16.0.0/16 | |
else if (ipBytes[0] == 172 && ipBytes[1] == 16) | |
{ | |
return true; | |
} | |
// 192.168.0.0/16 | |
else if (ipBytes[0] == 192 && ipBytes[1] == 168) | |
{ | |
return true; | |
} | |
// 169.254.0.0/16 | |
else if (ipBytes[0] == 169 && ipBytes[1] == 254) | |
{ | |
return true; | |
} | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment