Cant get proper client IP address on server side W

2019-08-01 05:19发布

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?

标签: c# wcf client ip
1条回答
Animai°情兽
2楼-- · 2019-08-01 05:45

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!

查看更多
登录 后发表回答