Click here to Skip to main content
12,511,553 members (49,184 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: WCF IP HTTPS
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.

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 15-Jun-12 1:12am
Updated 15-Jun-12 2:03am
Tim Corey113.1K
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
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.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160929.1 | Last Updated 15 Aug 2015
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100