9

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: https://web.archive.org/web/20211020102847/https://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.

2
  • 9
    I've seen this sample code repeated on many blogs. I'm amazed since it's so wrong.. part 1, get client's IP address. If that fails, return the server's IP address. wtf?!? Apr 5, 2010 at 16:07
  • 4
    Yep, just found that same example here: 4guysfromrolla.com/articles/071807-1.aspx. If nothing else, using ToString() to check for enum equality looks like it was written by a total .NET newbie.
    – vgru
    Dec 7, 2010 at 16:03

3 Answers 3

7

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.

1
  • 6
    And of course the obvious advice: IPv6 is coming, prepare your software to deal with it properly. Relying on IPv4 addresses is going to hurt you. Mar 14, 2013 at 15:43
7

I also had copied the example code and a colleague pointed out that it was obviously buggy. This line uses the host name of the server, hence the incorrect result:

foreach (IPAddress IPA in Dns.GetHostAddresses(Dns.GetHostName()))

I have corrected the code in my project as follows:

/// <summary>
/// Returns the IPv4 address of the specified host name or IP address.
/// </summary>
/// <param name="sHostNameOrAddress">The host name or IP address to resolve.</param>
/// <returns>The first IPv4 address associated with the specified host name, or null.</returns>
public static string GetIPv4Address(string sHostNameOrAddress)
{
  try
  {
    // Get the list of IP addresses for the specified host
    IPAddress[] aIPHostAddresses = Dns.GetHostAddresses(sHostNameOrAddress);

    // First try to find a real IPV4 address in the list
    foreach (IPAddress ipHost in aIPHostAddresses)
      if (ipHost.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
        return ipHost.ToString();

    // If that didn't work, try to lookup the IPV4 addresses for IPV6 addresses in the list
   foreach (IPAddress ipHost in aIPHostAddresses)
     if (ipHost.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6)
      {
        IPHostEntry ihe = Dns.GetHostEntry(ipHost);
        foreach (IPAddress ipEntry in ihe.AddressList)
          if (ipEntry.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
            return ipEntry.ToString();
      }
  }
  catch (Exception ex)
  {
    System.Diagnostics.Trace.WriteLine(ex);
  }
  return null;
}

The code above works in ASP.Net 2.0 on Windows 7/Server 2008. Hope this helps.

0
1

if you are using .Net 4.5 Framework then there is a method provide to convert IP6 to IP4

public IPAddress MapToIPv4()

You can find the details here

1
  • 6
    That method only makes sense to use on IPv6 addresses that have been obtained by mapping IPv4 addresses to v6 in the first place. As Jesse Weigert says, there is no magic v6 -> v4 converter.
    – AakashM
    May 3, 2013 at 15:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.