Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the IP address of a WCF remote endpoint?

Tags:

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 reflection, 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)

like image 557
Brann Avatar asked Dec 24 '08 09:12

Brann


2 Answers

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

like image 175
Marc Gravell Avatar answered Sep 27 '22 20:09

Marc Gravell


OperationContext context = OperationContext.Current; MessageProperties properties = context.IncomingMessageProperties; RemoteEndpointMessageProperty endpoint = properties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty; string address = endpoint.Address; 
like image 32
Cyrus Avatar answered Sep 27 '22 18:09

Cyrus