tags:

views:

937

answers:

1

I have some code in an asp.net app that needsto get the ipv4 address of the client computer (the users are all on our own network). Recently we upgraded the server the app runs on to windows 2008 server. Now the Request.UserHostAddress code returns the ipv4 when the client is on an older OS and ipv6 when they are on a newer OS (Vista and higher). So the feature that relys on this works for some clients and not others.

I added code that is supposed to convert from ipv6 to ipv4 to try to fix this problem. It's from this online tutorial: http://www.4guysfromrolla.com/articles/071807-1.aspx .I'm using dsn.GetHostAddress and then looping through the IPs returned looking for one that is "InterNetwork"

foreach (IPAddress IPA in Dns.GetHostAddresses(HttpContext.Current.Request.UserHostAddress))
{
    if (IPA.AddressFamily.ToString() == "InterNetwork")
    {
        IP4Address = IPA.ToString();
        break;
    }
}

if (IP4Address != String.Empty)
{
    return IP4Address;
}


foreach (IPAddress IPA in Dns.GetHostAddresses(Dns.GetHostName()))
{
    if (IPA.AddressFamily.ToString() == "InterNetwork")
    {
        IP4Address = IPA.ToString();
        break;
    }
}

return IP4Address;

The problem is that this isn't working for me. The clients connecting from ipv4 continue to return the correct ipv4 IP of the client computer, but the clients connecting from Vista and Windows 7 it is returning the ipv4 IP of the SERVER machine not the client computer.

+1  A: 

Simple answer: Disable IPV6 on the server, or remove the IPV6 address of the server from the DNS entry.

There is not a magic IPV4<->IPV6 converter. They're completely different protocols, and addresses in one don't translate to the other. If you want to reliably retrieve the IPV4 address of the client, you need to make sure that the client connects over IPV4.

Jesse Weigert