无法在服务器端WCF上获得正确的客户端IP地址 [英] Cant get proper client IP address on server side WCF

查看:672
本文介绍了无法在服务器端WCF上获得正确的客户端IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了在WCF中获取客户端IP,我使用以下方法:

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

我的机器有一些本地IPv4,这种方法一直工作到昨天。是我们的网络管理员改变了一些;我不知道,但现在 endpointProperty.Address 返回:: 1而不是xxx.xxx.xxx.xxx。有人可以解释一下会导致这种情况的原因吗?

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?

推荐答案

这是因为您的机器现在使用IPv6环回而不是IPv4。

This is because your machine is now using IPv6 loopback instead of IPv4.

它还会影响内部网IP地址,因为您的管理员可能已经全面启用了IPv6 - 因此机器A将使用它的IPv6地址向机器B标识自己。

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.

在公共环境中,在整个互联网转移到IPv6之前,它不太可能导致问题。

In a public environment it's unlikely to cause a problem until the entire interweb moves to IPv6.

无论哪种方式,你都应该确保你使用 IPAddress 中的方法来解析端点的IP而不是自己动手。

Either way, you should ensure that you use the methods in IPAddress to parse the endpoint's IP rather than hand-cranking your own.

我也得到了当我设置一个用于跟踪IP地址为varchar(15)的数据库列时,遇到了类似的问题;工作greta,直到我们内部发生同样的事情,突然我的所有请求记录开始打破内部请求!

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!

这篇关于无法在服务器端WCF上获得正确的客户端IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆