Skip to content

Instantly share code, notes, and snippets.

@lindexi
Created December 4, 2016 01:16
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 lindexi/803867f7ddae1cf6e8f53b70629b0898 to your computer and use it in GitHub Desktop.
Save lindexi/803867f7ddae1cf6e8f53b70629b0898 to your computer and use it in GitHub Desktop.
/// <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