C#.NET determine if a computer, hostname or IP address resolve to the local machine

When you're using a .NET application you may wish to alter how the application behaves depending on whether the machine you're connecting to is a local or remote machine.

This is part of our IT Documentation Tool, XIA Configuration Server.

It doesn't seem that there's a built in way to determine this with .NET

/// <summary>
/// Determines if the specified address or hostname belongs to the local machine.
/// </summary>
/// <param name="Hostname">The IP address or hostname to validate.</param>
/// <returns>A System.Boolean value that indicates whether the address is local.</returns>
public static bool IsLocalMachine(String Hostname)
{
    if (String.IsNullOrEmpty(Hostname)) { return false; }
    Hostname = Hostname.Trim().ToLower();
    IPAddress[] ResolvedAddresses;
    try { ResolvedAddresses = System.Net.Dns.GetHostAddresses(Hostname); }
    catch (Exception) { return false; }
    foreach (NetworkInterface Interface in NetworkInterface.GetAllNetworkInterfaces())
    {
        foreach (UnicastIPAddressInformation LocalAddress in Interface.GetIPProperties().UnicastAddresses)
        {
            if (StringSupport.Equals(Hostname, LocalAddress.ToString())) { return true; }
            foreach (IPAddress ResolvedAddress in ResolvedAddresses)
            {
                if (StringSupport.Equals(ResolvedAddress.ToString(), LocalAddress.Address.ToString())) { return true; }
            }
        }
    }
    return false;
}



The method above resolves the address to an IP address and then checks this against each of the IP addresses on all network interfaces on the local machine. 

The method will return correctly the following scenarios

  • "Localhost" is used
  • The IPv4 or IPv6 localhost IP address is entered (127.0.0.1, ::1
  • Any IPv4 or IPv6 IP address is entered that is assigned to any of the machines network interfaces
  • The computer's NetBIOS or DNS name is entered
  • Any NetBIOS or DNS name that the computer resolves to one of it's own addresses is entered.



The StringSupport.Equals() method simply perfoms a case insensitive string comparison.

/// <summary>
/// Compares the two strings for equality ignoring case and whitespace around the string.
/// </summary>
/// <param name="strA">The first string to compare.</param>
/// <param name="strB">The second string to compare.</param>
/// <returns>A System.Boolean value that determines if the two strings are equal.</returns>
public static bool Equals(String strA, String strB)
{
    if (strA == null) strA = String.Empty;
    if (strB == null) strB = String.Empty;
    strA = strA.Trim();
    strB = strB.Trim();
    return strA.Equals(strB, StringComparison.CurrentCultureIgnoreCase);
}

Comments

Popular posts from this blog

Windows Server 2016, 2019, 2022, Windows 10 and Windows 11: Date and time "Some settings are managed by your organization".

TFTPD32 or TFTPD64 reports Bind error 10013 An attempt was made to access a socket in a way forbidden by its access permissions.

Windows Server 2019 desktop icons such as My Computer, Windows cannot access the specified device, path, or file.