Created
May 26, 2015 09:01
-
-
Save RaccoonDev/c196ec17c23a9998793d to your computer and use it in GitHub Desktop.
Check is DNS points to local IP address
This file contains 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
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