无法在服务器端 WCF 上获取正确的客户端 IP 地址
为了在 WCF 中获取客户端 IP,我使用以下方法:
public static byte[] GetUserIP(OperationContext context)
{
var messageProperties = context.IncomingMessageProperties;
RemoteEndpointMessageProperty endpointProperty =
messageProperties[RemoteEndpointMessageProperty.Name]
as RemoteEndpointMessageProperty;
return GetIPFromString(endpointProperty.Address);
}
我的机器有一些本地 IPv4,并且此方法直到昨天才有效。可能是我们的网络管理员更改了某些内容;我不知道,但现在 endpointProperty.Address
返回“::1”而不是“xxx.xxx.xxx.xxx”。有人可以解释什么会导致这样的事情吗?
For obtaining client IP in WCF i use the following method:
public static byte[] GetUserIP(OperationContext context)
{
var messageProperties = context.IncomingMessageProperties;
RemoteEndpointMessageProperty endpointProperty =
messageProperties[RemoteEndpointMessageProperty.Name]
as RemoteEndpointMessageProperty;
return GetIPFromString(endpointProperty.Address);
}
My machine has some local IPv4 and this method was working until yesterday.. May be our network admins changed something; i dont know, but now the endpointProperty.Address
is returning "::1" and not "xxx.xxx.xxx.xxx". Can someone explain what can cause such thing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为您的计算机现在使用 IPv6 环回而不是 IPv4。
它还会影响 Intranet IP 地址,因为您的管理员可能全面启用了 IPv6 - 因此机器 A 将使用其 IPv6 地址向机器 B 标识自己。
在公共环境中,除非整个互联网转向 IPv6,否则不太可能造成问题。
无论哪种方式,您都应该确保使用
IPAddress
中的方法来解析端点的 IP,而不是手动启动您自己的 IP。当我设置一个用于跟踪 IP 地址的数据库列时,我也遇到了类似的问题 varchar(15);格蕾塔一直在工作,直到我们内部发生了同样的事情,突然之间,我的所有请求记录开始打破内部请求!
This is because your machine is now using IPv6 loopback instead of IPv4.
It will also affect intranet IP addresses as your Admins have likely enabled IPv6 across the board - so Machine A will identify itself to Machine B with it's IPv6 address.
In a public environment it's unlikely to cause a problem until the entire interweb moves to IPv6.
Either way, you should ensure that you use the methods in
IPAddress
to parse the endpoint's IP rather than hand-cranking your own.I also got caught out by a similar problem when I set up a database column that would be used to track IP addresses as varchar(15); worked greta until the same thing happened to us internally and all of a sudden all my request logging starting breaking on internal requests!