HttpContext.Current.Request.ServerVariables[“REMOTE_ADDR”] 正在返回 IPv6

发布于 2024-12-03 19:57:02 字数 199 浏览 4 评论 0原文

Microsoft 的 HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"] 正在为远程客户端返回 IPv6。但是,我需要将此数据用于会话日志记录表,其中 ClientIP 列是 varchar(15)...IOW,我需要 IPv4 客户端 IP 地址,因为 IPv6 会引发字符串截断错误。

这可行吗?

Microsoft's HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"] is returning IPv6 for the remote client. However, I need to use this data for a session logging table where the ClientIP column is varchar(15)... IOW, I need the IPv4 client IP address as IPv6 throws a string truncation error.

Is this doable?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

洒一地阳光 2024-12-10 19:57:02

不,客户端的 IPv4 和 IPv6 地址完全无关,因此当某人通过 IPv6 连接时,您无法找到他们的 IPv4 地址。也许他们甚至没有 IPv4 地址……或者至少没有唯一的 IPv4 地址。越来越多的 ISP 将开始在客户之间共享 IPv4 地址,因为他们没有足够的 IPv4 地址来为每个用户提供自己的地址。这还意味着,如果用户的 IPv4 地址来自客户共享的地址池,则该用户的 IPv4 地址可能会随着时间而改变。

当然,您可以尝试让用户通过 IPv4 进行连接。但就像我说的:不能保证这会给你带来有用的东西。在(不久的)将来,情况会变得更糟......

No, the IPv4 and IPv6 addresses of a client are completely unrelated, so you cannot find someone's IPv4 address when they connect over IPv6. Maybe they don't even have an IPv4 address... Or at least not an IPv4 address that is unique. More and more ISPs will start to share IPv4 addresses between customers because they don't have enough IPv4 addresses to give every user his own. That also means that a user's IPv4 address can change over time, if the IPv4 address comes out of a pool of addresses that customers share.

You could try to make the user connect over IPv4 of course. But like I said: no guarantee that that will give you something useful. And in the (near) future it will become worse...

雪若未夕 2024-12-10 19:57:02

下面的代码获取服务器上的所有 IP 地址,因为服务器可能有多个网络接口。

string ipList = "";
IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName()); 
List<IPAddress> ipAddressList = ipHostInfo.AddressList.ToList();
foreach (IPAddress ip in ipAddress)
{
    if (ips.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)//This will check if the IP is an ipv4.
       ipList += ips;
}
return ip.ToString();

The below code gets all the IP addresses on the server, as the server may have multiple network interface.

string ipList = "";
IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName()); 
List<IPAddress> ipAddressList = ipHostInfo.AddressList.ToList();
foreach (IPAddress ip in ipAddress)
{
    if (ips.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)//This will check if the IP is an ipv4.
       ipList += ips;
}
return ip.ToString();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文