Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi all,
I want to get the client IP address in a WCF Service hosted using HTTPS 443 bindings. I am getting the Host IP address by below code.

C#
private string GetIP()
{
   OperationContext context = OperationContext.Current;
   MessageProperties prop = context.IncomingMessageProperties;
   RemoteEndpointMessageProperty endpoint =
      prop[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
   string ip = endpoint.Address;

   return ip ;

}


My host server is under the firewall access does it create any problem for the WCF Service to read the correct client IP address.

Please help if you have any suggestions on this.

Thanks in advance.

Regards.

Reishabh
Posted
Updated 15-Jun-12 2:03am
v2

There doesn't appear to be a reliable way to get the client IP for a WCF call using .Net 3. In .Net 3.5 you can and it's done exactly how you've described in your question.

If your client IP is on the same network you should be able to get the IP. But if it's not it's important to remember how internet routing and network address tables (NAT) work.

If your clients are not connected directly to the internet and is accessing it via a router, it's possible that many of your clients could be sharing the same IP address as the only visible IP address will be the public IP assigned to the network by their internet service provider (This is often called a none NAT address or real address).

In larger corporate networks this can be further obfuscated as they could have multiple levels of NATs between you and your client.

If that wasn't enough, if your client is behind a load balanced proxy, you could find that the same client doesn't always come from the same ip address. An IPv4 address is split into four bytes. 192.168.0.1. 192 is the class A, 168 is the class B, 0 is the class C and the 1 is the class D part. You may find your client's subsequent requests are coming from multiple class D addresses. This is also an issue you come across when configuring load balanced web servers which require server affinity for state management. If you don't configure the affinity to class C addresses (So all matching class C address go to the same server) then you'll soon run into state management issue with corporate users.
 
Share this answer
 
Comments
Reishabh_Saxena 16-Jun-12 2:23am    
Can firewall create problem in getting correct clients IP address ?
Stephen Hewison 16-Jun-12 4:05am    
No. The primary role of a firewall is to restrict the ports which and connect into a network. The information about the client ip is passed with the request. If you're receiving the request then you're receiving the ip information being passed by the client. Your problem will be somewhere else.
One thing to note if you're using async/await -- you need to store OperationContext.Current into a local variable at the beginning of your service method, and pass it along, since it will be null after the first await in your async method.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900