简体   繁体   English

wcf client ip as ipv6

[英]wcf client ip as ipv6

i'm using next piece of code to get client ip on wcf service : 我正在使用下一段代码在wcf服务上获取客户端ip:

        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 were ok, endpoint.Address returned ipv4. 虽然这段代码在iis6 / server2003上运行,但一切都还可以,endpoint.Address返回了ipv4。 but after i recently updated to iis7/server2008 endpoint.Address is returning ipv6. 但在我最近更新到iis7 / server2008 endpoint.Address后返回ipv6。

is it still possible to get ipv4 on iis7/server2008 ? 是否仍然可以在iis7 / server2008上获得ipv4?

Thank You ! 谢谢 !

This is not so much of a change in WCF that is a change in networking. 这不是WCF的变化,而是网络的变化。 Your client has used its IPv6 to connect to the server and that is the address which is stored in the context of the message. 您的客户端已使用其IPv6连接到服务器,这是存储在消息上下文中的地址。 If you need to get hold of IPv4, use the snippet below: 如果您需要获取IPv4,请使用以下代码段:

    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. 这会将您的IPv6转换为IPv4。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM