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.
微软的HttpContext.Current.Request.ServerVariables[“REMOTE_ADDR”]正在为远程客户端返回ipv6。但是,我需要将此数据用于一个会话日志表,其中的ClientIP列是varchar(15)...我需要IPv4客户端IP地址,因为IPv6引发字符串截断错误。
Is this doable?
这可行吗?
更多回答
You cannot find out a user's IPv4 address solely basing it on their IPv6 address. Total impossibility. (In rare circumstances (and only supported on non-Windows operating systems) the IPv6 address is included in the address like ::ffff:1.2.3.4
when the application uses a single dual-stack socket for binding both v4+v6, but that is a very unlikely scenario in your circumstance – especially given that, like I said, it's not supported on Windows.)
您不能仅根据用户的IPv6地址来查找用户的IPv4地址。完全不可能。(在极少数情况下(仅在非Windows操作系统上受支持),当应用程序使用单个双堆栈套接字绑定v4+v6时,IPv6地址包括在地址中,如:ffff:1.2.3.4,但在您的情况下这是非常不可能的情况--特别是考虑到,正如我所说的,Windows不支持它。)
优秀答案推荐
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.
不是,客户端的IPv4和IPv6地址完全无关,所以当某人通过IPv6连接时,您无法找到他们的IPv4地址。也许他们甚至没有IPV4地址。或者至少不是唯一的IPv4地址。越来越多的互联网服务提供商将开始在客户之间共享IPv4地址,因为他们没有足够的IPv4地址来为每个用户提供自己的地址。这也意味着,如果用户的IPv4地址来自客户共享的地址池,则该地址可能会随着时间的推移而变化。
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...
当然,您可以尝试让用户通过IPv4进行连接。但正如我所说的:不能保证这会给你带来一些有用的东西。在不久的将来,情况会变得更糟。
The below code gets all the IP addresses on the server, as the server may have multiple network interface.
下面的代码获取服务器上的所有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();
更多回答
我是一名优秀的程序员,十分优秀!