C# IIS Request.UserHostAddress 返回 IPV6 (::1),即使禁用 IPV6

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1932843/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 21:57:38  来源:igfitidea点击:

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

c#asp.netipv6ipv4

提问by maxp

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

在我的网卡的属性部分,在 Windows Server 2008 上,我禁用了 IPV6,只启用了 IPV4。

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

但是在 ASP.NET 中,Request.UserHostAddress 返回 '::1',一个 IPV6 地址。

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

有没有人知道如何恢复到 IPV4?

采纳答案by Miguel Ventura

If you're connecting to localhost (::1 / 127.0.0.1), you're not using the network card that your server has, but rather like a virtual card that windows has. I don't think there is anyway to configure the loopback card and removing IPv6 from it, not without removing support from the whole system, but in Win2008 you probably can't do that anymore.

如果您连接到 localhost (::1 / 127.0.0.1),则您使用的不是服务器的网卡,而是 Windows 的虚拟网卡。我认为无论如何都不需要配置环回卡并从中删除 IPv6,不是不删除整个系统的支持,但在 Win2008 中你可能不能再这样做了。

You can verify that your physical card isn't being used by running network packet capturing utils. In windows, you can never sniff out the traffic that walks the virtual loopback card.

您可以通过运行网络数据包捕获实用程序来验证您的物理卡未被使用。在 Windows 中,您永远无法嗅出经过虚拟环回卡的流量。

That said, should you access from a different machine (through a connection that will be passing through your physical card), you should see an IPv4 address being returned by Request.UserHostAddress

也就是说,如果您从另一台机器访问(通过将通过您的物理卡的连接),您应该看到一个 IPv4 地址由 Request.UserHostAddress

回答by David

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

来自 Rolla 网站的 4 Guys 在这里有一个解决方案,我在我的应用程序中使用过。

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

回答by Rageesh Geetha Raman

  1. Go to Start -> click on Control Panel -> click on View network status and tasksin Control Panel window -> click on change adapter settingsin Network and Sharing Centerwindow.

  2. Network Connectionswindow will appear. Here you right click on the network adapter(can be wireless adapter or wired Ethernet adapter) that you wish to disable IPv6 and click Properties.

  3. In the network adapter propertieswindow, untick Internet Protocol Version 6 (TCP/IPv6)and click OK.

  4. Type regeditin command prompt to launch registry editor, proceed to locate following registry item in Registry editor.

    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip6\Parameters\DisabledComponents
    
  5. If the DisabledComponentsitem is unavailable, you must create it in following format by going to Edit -> New -> DWORD (32-bit) Value and set the value to 0xffffffff(just type ffffffff). This will disable all IPv6 features except the IPv6 loopback interface, finally click OK.

  6. Restart the computer.

  1. 转到开始 -> 单击控制面板 ->View network status and tasks在控制面板窗口中单击 ->change adapter settingsNetwork and Sharing Center窗口中单击。

  2. Network Connections将出现窗口。在此处右键单击network adapter要禁用 IPv6的(可以是无线适配器或有线以太网适配器),然后单击Properties

  3. network adapter properties窗口中,取消勾选Internet Protocol Version 6 (TCP/IPv6)并单击确定。

  4. 键入regedit命令提示符以启动注册表编辑器,继续在注册表编辑器中找到以下注册表项。

    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip6\Parameters\DisabledComponents
    
  5. 如果该DisabledComponents项目不可用,则必须通过转到 Edit -> New -> DWORD (32-bit) Value 并将值设置为0xffffffff(只需键入ffffffff)以以下格式创建它。这将禁用除 IPv6 环回接口之外的所有 IPv6 功能,最后单击OK

  6. 重新启动计算机。