領導又有了新的要求,在用戶登錄的時候記錄用戶的IP地址和機器名。

     在C#中,獲取本機的機器名和IP地址,甚至網卡的MAC地址,都是很簡單的事情,有封裝好的類和方法可以使用,但現在遇到的問題是,如果客戶端的機器是在另一個局域網內,那網卡的IP地址是局域網地址,這對我們來說沒有用,這就要記錄外網的IP,但是Remoting沒有現成的方法來獲取客戶端的IP,google了一下,網上比較多的是采用在Server端注冊自定義Server Channel Sink,通過Transport Headers來獲取Request的IP。于是我就用了別人寫好的現成的Sink,以及學習了Rickie的一篇《如何定制Sink擴展.Net Remoting功能》。
     Rickie的文章:http://www.cnblogs.com/rickie/archive/2004/10/21/54891.html
     自定義的Sink的類:http://blog.joycode.com/tingwang/articles/39610.aspx

 

 

Remoting獲取客戶端外網IPpublic class ClientIPServerSinkProvider : IServerChannelSinkProvider
    }

 

     在Remoting的服務端加入了這個類后,由于我是在IIS上承載的Remoting,所以就需要修改Web.config文件來啟用這個Sink。

 

 

Remoting獲取客戶端外網IP      <channels>
Remoting獲取客戶端外網IP        
<channel port="8000" ref="http" name ="httpServer">
Remoting獲取客戶端外網IP          
<serverProviders>
Remoting獲取客戶端外網IP            
<formatter ref="soap" typeFilterLevel="Full"/>
Remoting獲取客戶端外網IP            
<formatter ref="binary" typeFilterLevel="Full"/>
Remoting獲取客戶端外網IP            
<provider type="Galaxy.Server.Remoting.ClientIPServerSinkProvider, Galaxy.Server.Remoting" />
Remoting獲取客戶端外網IP          
</serverProviders>
Remoting獲取客戶端外網IP        
</channel>
Remoting獲取客戶端外網IP        
<channel port="9000" ref="tcp" name ="tcpServer">
Remoting獲取客戶端外網IP          
<serverProviders>
Remoting獲取客戶端外網IP            
<formatter ref="soap" typeFilterLevel="Full"/>
Remoting獲取客戶端外網IP            
<formatter ref="binary" typeFilterLevel="Full"/>
Remoting獲取客戶端外網IP            
<provider type="Galaxy.Server.Remoting.ClientIPServerSinkProvider, Galaxy.Server.Remoting" />
Remoting獲取客戶端外網IP          
</serverProviders>
Remoting獲取客戶端外網IP        
</channel>
Remoting獲取客戶端外網IP      
</channels>

 

     做完這個后,以后只需在服務器端通過 ((System.Net.IPAddress)System.Runtime.Remoting.Messaging.CallContext.GetData("ClientIPAddress")).ToString() 就可以獲取Request的IP了。

相关文章:

  • 2022-01-14
  • 2021-08-30
  • 2021-11-08
  • 2021-09-04
  • 2021-10-02
猜你喜欢
  • 2022-02-02
  • 2021-05-23
  • 2022-12-23
  • 2021-10-02
  • 2022-01-16
  • 2021-05-17
  • 2022-12-23
相关资源
相似解决方案