gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 04:10:44 27 4
gpt4 key购买 nike

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

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

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

最佳答案

Rolla 网站的 4 个人有一个解决方案 here ,我已经在我的应用中使用了。

更新:

以防万一此链接失效,这里是基于此链接的代码:

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

关于c# - IIS Request.UserHostAddress 返回 IPV6 (::1),即使 IPV6 已禁用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1932843/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com