Cant get proper client IP address on server side WCF

﹥>﹥吖頭↗ 提交于 2019-12-11 12:52:02

问题


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?


回答1:


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!



来源:https://stackoverflow.com/questions/5311684/cant-get-proper-client-ip-address-on-server-side-wcf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!