Skip to content

Instantly share code, notes, and snippets.

@RaccoonDev
Created May 26, 2015 09:01
Show Gist options
  • Save RaccoonDev/c196ec17c23a9998793d to your computer and use it in GitHub Desktop.
Save RaccoonDev/c196ec17c23a9998793d to your computer and use it in GitHub Desktop.
Check is DNS points to local IP address
public static bool IsLocalhost(string hostNameOrAddress)
{
if (string.IsNullOrEmpty(hostNameOrAddress))
return false;
try
{
// get host IP addresses
IPAddress[] hostIPs = Dns.GetHostAddresses(hostNameOrAddress);
// get local IP addresses
IPAddress[] localIPs = Dns.GetHostAddresses(Dns.GetHostName());
// test if any host IP is a loopback IP or is equal to any local IP
return hostIPs.Any(hostIP => IPAddress.IsLoopback(hostIP) || localIPs.Contains(hostIP));
}
catch
{
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment