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();
        }
    }
}
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM