问题
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:
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.
回答2:
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.
回答3:
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
来源:https://stackoverflow.com/questions/2043769/problem-converting-ipv6-to-ipv4