WCF 获取客户端IP 整理

以前刚学习WCF时记得只能获得客户端内网IP,现在忘记了,不知道怎么获取。

  IPHostEntry v = System.Net.Dns.Resolve(System.Net.Dns.GetHostName());
            Console.WriteLine(System.Net.Dns.Resolve(System.Net.Dns.GetHostName()).AddressList.GetValue(0).ToString());//.Current.Request.UserHostAddress;

          string hostName=  System.Net.Dns.GetHostName();
            IPHostEntry ipHostEntry= System.Net.Dns.GetHostEntry(hostName  );
          这些获得的是所有内网IP,但多个网卡时,就不知道当前客户端用的那个IP了。

请大家指点!非常感谢! 

 

下面是获得外网IP,以前找了很久都没找到。

 //提供方法执行的上下文环境
            OperationContext context = OperationContext.Current;
            //获取传进的消息属性
            MessageProperties properties = context.IncomingMessageProperties;
            //获取消息发送的远程终结点IP和端口
            RemoteEndpointMessageProperty endpoint = properties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;


            Console.WriteLine(string.Format("Hello {0},You are   from {1}:{2} 内网ip={3}", name, endpoint.Address, endpoint.Port

 

 

Try using the following code.

using System.ServiceModel;
using System.ServiceModel.Channels;
 OperationContext context = OperationContext.Current;
 MessageProperties prop = context.IncomingMessageProperties;
 RemoteEndpointMessageProperty endpoint = prop[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
 string ip = endpoint.Address;
 
namespace Sample
{
    using System;
    using System.ServiceModel;
    using System.ServiceModel.Channels;
    using System.Net;
 
    [ServiceContract]
    interface IMyService
    {
        [OperationContract]
        public string ProcessUntyped(Message message);
        [OperationContract]
        public string ProcessTyped(string str);
    }
 
    class MyService : IMyService
    {
        public string ProcessUntyped(Message message)
        {
            RemoteEndpointMessageProperty endpoint = message.Properties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
            return string.Format("Connected from {0}:{1}", str, endpoint.Address, endpoint.Port);
        }
 
        public string ProcessTyped(string str)
        {
            OperationContext context = OperationContext.Current;
            MessageProperties properties = context.IncomingMessageProperties;
            RemoteEndpointMessageProperty endpoint = properties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
            return string.Format("You said: '{0}'. Connected from {1}:{2}", str, endpoint.Address, endpoint.Port);
        }
    }
}

获取客户端IP的代码:

 /*************************************************************************************
 * 代码:吴蒋
 * 时间:2012.02.07
 * 说明:安全类
 * 其他:
 * 修改人:
 * 修改时间:
 * 修改说明:
 ************************************************************************************/
using System.ServiceModel;
using System.ServiceModel.Channels;
 
namespace Tools
{
    public class Safe
    {
        public static Safe Instance()
        {
            return new Safe();
        }
 
        public string ClientIp()
        {             
            OperationContext context = OperationContext.Current;
            MessageProperties properties = context.IncomingMessageProperties;
            RemoteEndpointMessageProperty endpoint = properties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
            return endpoint.Address;
        }
 
        public string ClientPort()
        { 
            OperationContext context = OperationContext.Current;
            MessageProperties properties = context.IncomingMessageProperties;
            RemoteEndpointMessageProperty endpoint = properties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
            return endpoint.Port.ToString();
        }
 
        public string ClientIpAndPort()
        {
            OperationContext context = OperationContext.Current;
            MessageProperties properties = context.IncomingMessageProperties;
            RemoteEndpointMessageProperty endpoint = properties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
            return endpoint.Address + ";" + endpoint.Port.ToString();
        }
    }
}
 

你可能感兴趣的:(WCF)