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

maxp picture maxp · Dec 19, 2009 · Viewed 34.9k times · Source

In the properties section of my network card, on windows server 2008, i have IPV6 disabled, leaving only IPV4 enabled.

However in ASP.NET, Request.UserHostAddress returns '::1', an IPV6 address.

Has anyone got any idea how to revert back to IPV4?

Answer

David picture David · Dec 21, 2011

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

Update:

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();
}