Brann
Brann

Reputation: 32376

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

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)

Upvotes: 25

Views: 15797

Answers (3)

Cyrus
Cyrus

Reputation: 2429

OperationContext context = OperationContext.Current;
MessageProperties properties = context.IncomingMessageProperties;
RemoteEndpointMessageProperty endpoint = properties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
string address = endpoint.Address;

Upvotes: 5

Marc Gravell
Marc Gravell

Reputation: 1062560

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

Upvotes: 15

Joachim Kerschbaumer
Joachim Kerschbaumer

Reputation: 9871

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

Upvotes: 2

Related Questions