Thứ Sáu, 29 tháng 6, 2012

Lấy địa chỉ Ip của client truy cập tới web server ASP.NET, Webservice, WCF

Lấy địa chỉ Ip của client truy cập có thể dùng cho nhiều mục đích khác nhau: thống kê số lượng ip truy cập, chặn ip, 1 user đã sử dụng bao nhiêu địa chỉ ip để đăng nhập website ...

Ứng dụng web ASP.NET thông thường:
  1. string userIp = Request.UserHostAddress;  
Webservice:
  1. string userIp = Context.Request.UserHostAddress;  
WCF:
  1. public string GetClientIp()  
  2. {  
  3.     RemoteEndpointMessageProperty endpoint = OperationContext.Current.IncomingMessageProperties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;  
  4.     return endpoint.Address;  
  5. }  

1 nhận xét :