Wcf client ip as ipv6

I use the following code snippet to get client ip on wcf service:

        OperationContext context = OperationContext.Current;
        System.ServiceModel.Channels.MessageProperties prop = context.IncomingMessageProperties;
        System.ServiceModel.Channels.RemoteEndpointMessageProperty endpoint = prop[System.ServiceModel.Channels.RemoteEndpointMessageProperty.Name] as System.ServiceModel.Channels.RemoteEndpointMessageProperty;
        string ip = endpoint.Address;

while this code worked on iis6 / server2003, everything was fine, endpoint.Address returned ipv4. but after I recently upgraded to the iis7 / server2008 endpoint. Add returns ipv6.

is it possible to get ipv4 on iis7 / server2008?

Thank!

+3
source share
1 answer

This is not so much a change to WCF as a change to the network. Your client used their IPv6 to connect to the server, and this is the address that is stored in the context of the message. If you need to get IPv4, use the snippet below:

    IPAddress ipAddress = IPAddress.Parse(ipv6);
    IPHostEntry ipHostEntry = Dns.GetHostEntry(ipAddress);
    foreach (IPAddress address in ipHostEntry.AddressList)
    {
           if(address.AddressFamily == AddressFamily.InterNetwork)
                  Console.WriteLine(address);
    }

This will translate your IPv6 to IPv4.

+3
source

Source: https://habr.com/ru/post/1754772/


All Articles