views:

1781

answers:

2

Is there a way to get the remote IP Address of a WCF connection?

I guess the reason why it's not built-in into the WCF framework is that WCF can work with non TCP/IP bindings, so the IP Address is not always meaningful.

However, the information would make sense for all the widely used bindings (As far as I know : BasicHttp, DualHttp, WSHttp and NetTcp).

The IP address is probably accessible using reflexion, but I'd rather find a documented way to get it rather than hacking into the framework classes.

I've googled on the issue, and it seems a lot of people have run into it without finding a decent solution (The usual answer is to rely on the message headers, but this implies trusting the client to provide its real IP Address, which is not an option if you want to log the IP Address for security reasons)

+1  A: 

if you're on the service side and want to get the client IP, you have to check the OperationContext's Message Properties. look here or here for example

Joachim Kerschbaumer
If I'm not mistaking, this implies trusting the Client (see the end of the first linked post : "We don't do any sort of spoof detection, so be careful about using the IP for any sort of security decisions.")I'd rather access my own socket and retrieve the IP address it's connected to.
Brann
+5  A: 

Apparently it has been added in 3.5 via RemoteEndpointMessageProperty; see here.

Marc Gravell
the post says this can be spoofed, but I can't figure out whether they are talking about classic IP spoofing (using syn/acks) or if they mean the info is provided by the remote wcf application. I think it's rather the former because the address we get is the proxy/relay address. Any hint on this?
Brann
Sorry, no I don't.
Marc Gravell
The example produces the IP in IPv6 format - anyone know how to convert it to an IPv4 address?
Luke Venediger
@Luke [See this question](http://stackoverflow.com/questions/2028879/ipv4-remote-address-in-wcf "IPv4 remote address in WCF")
Wilfred Knievel