Retrieve IP and Port number of remote endpoint request sender

I wanted to track which IP addresses were sending requests to my WCF service. So I did some investigation and apparently it’s not so hard to retrieve this data. The RemoteEndpointMessageProperty class makes the client IP address and port number associated with the remote endpoint from which a message was sent available. Underneath is a snippet of code that will show how it works.

   1: public string TrackIP()
   2: {
   3:     OperationContext oc = OperationContext.Current;
   4:     MessageProperties mp = oc.IncomingMessageProperties;
   5:     RemoteEndpointMessageProperty remp = mp[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
   6:  
   7:     return string.Format("Request sent from {0} and port is {1}", remp.Address, remp.Port);
   8: }

No comments yet. Be the first.

Leave a reply

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word