Cant get proper client IP address on server side W

2019-08-01 04:53发布

问题:

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!



标签: c# wcf client ip