Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将ipv6转换为ipv4时出现问题_C#_Asp.net_Ipv6 - Fatal编程技术网

C# 将ipv6转换为ipv4时出现问题

C# 将ipv6转换为ipv4时出现问题,c#,asp.net,ipv6,C#,Asp.net,Ipv6,我在asp.net应用程序中有一些代码,需要获取客户端计算机的ipv4地址(用户都在我们自己的网络上)。最近,我们将应用程序运行的服务器升级到windows 2008 server。现在,当客户端位于旧操作系统上时,Request.UserHostAddress代码返回ipv4;当客户端位于新操作系统(Vista及更高版本)上时,返回ipv6。因此,依赖于此的功能适用于某些客户机,而不适用于其他客户机 我添加了从ipv6转换到ipv4的代码,试图解决这个问题。这是本在线教程中的内容:。我正在使用

我在asp.net应用程序中有一些代码,需要获取客户端计算机的ipv4地址(用户都在我们自己的网络上)。最近,我们将应用程序运行的服务器升级到windows 2008 server。现在,当客户端位于旧操作系统上时,Request.UserHostAddress代码返回ipv4;当客户端位于新操作系统(Vista及更高版本)上时,返回ipv6。因此,依赖于此的功能适用于某些客户机,而不适用于其他客户机

我添加了从ipv6转换到ipv4的代码,试图解决这个问题。这是本在线教程中的内容:。我正在使用dsn.GetHostAddress,然后在返回的IP中循环查找一个“InterNetwork”


问题是这对我不起作用。从ipv4连接的客户端继续返回客户端计算机的正确ipv4 IP,但从Vista和Windows 7 it连接的客户端返回的是服务器计算机而不是客户端计算机的ipv4 IP

简单回答:在服务器上禁用IPV6,或从DNS条目中删除服务器的IPV6地址


没有神奇的IPV4IPV6转换器。它们是完全不同的协议,一个协议中的地址不能转换为另一个协议。如果要可靠地检索客户端的IPV4地址,需要确保客户端通过IPV4连接。

如果您使用的是.Net 4.5 Framework,则提供了将IP6转换为IP4的方法

public IPAddress MapToIPv4()

您可以找到详细信息

我还复制了示例代码,一位同事指出它显然有缺陷。 此行使用服务器的主机名,因此结果不正确:

foreach (IPAddress IPA in Dns.GetHostAddresses(Dns.GetHostName()))
我已将项目中的代码更正如下:

/// <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;
}
//
///返回指定主机名或IP地址的IPv4地址。
/// 
///要解析的主机名或IP地址。
///与指定主机名关联的第一个IPv4地址,或null。
公共静态字符串GetIPv4Address(字符串sHostNameOrAddress)
{
尝试
{
//获取指定主机的IP地址列表
IPAddress[]aiphostadAddresses=Dns.GetHostAddresses(sHostNameOrAddress);
//首先尝试在列表中找到一个真实的IPV4地址
foreach(AipHost中的IPAddress ipHost)
if(ipHost.AddressFamily==System.Net.Sockets.AddressFamily.InterNetwork)
返回ipHost.ToString();
//如果不起作用,请尝试在列表中查找IPV6地址的IPV4地址
foreach(AipHost中的IPAddress ipHost)
if(ipHost.AddressFamily==System.Net.Sockets.AddressFamily.InterNetworkV6)
{
IPHostEntry ihe=Dns.GetHostEntry(ipHost);
foreach(ihe.AddressList中的IPAddress iEntry)
if(ipEntry.AddressFamily==System.Net.Sockets.AddressFamily.InterNetwork)
返回ipEntry.ToString();
}
}
捕获(例外情况除外)
{
系统诊断跟踪写入线(ex);
}
返回null;
}
上面的代码在Windows7/Server2008上的ASP.NET2.0中工作。
希望这能有所帮助。

我已经在许多博客上看到了重复的示例代码。我很惊讶,因为它错得太多了。。第1部分,获取客户端的IP地址。如果失败,请返回服务器的IP地址。世界跆拳道联盟?!?是的,在这里找到了同样的例子:。如果没有其他问题,那么使用
ToString()
检查枚举相等性看起来就像是由一个完全的.NET新手编写的。当然,显而易见的建议是:IPv6即将到来,请准备好您的软件以正确处理它。依赖IPv4地址会对您造成伤害。这种方法只适用于通过将IPv4地址映射到v6而获得的IPv6地址。正如Jesse Weigert所说,没有神奇的v6->v4转换器。
/// <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;
}