IIS Request.UserHostAddress returning IPV6 (::1), even when IPV6 disabled

The 4 Guys from Rolla website has a solution here, which I’ve used in my app.


Just in case this link goes dead, here is code based on this link:

public string GetIpAddress()
    string ipAddressString = HttpContext.Current.Request.UserHostAddress;

    if (ipAddressString == null)
        return null;

    IPAddress ipAddress;
    IPAddress.TryParse(ipAddressString, out ipAddress);

    // If we got an IPV6 address, then we need to ask the network for the IPV4 address 
    // This usually only happens when the browser is on the same machine as the server.
    if (ipAddress.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6)
        ipAddress = System.Net.Dns.GetHostEntry(ipAddress).AddressList
            .First(x => x.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork);

    return ipAddress.ToString();

Leave a Comment