82

Apparently you can easily obtain a client IP address in WCF 3.5 but not in WCF 3.0. Anyone know how?

Gaz
  • 4,064
  • 3
  • 32
  • 34

3 Answers3

152

This doesn't help you in 3.0, but I can just see people finding this question and being frustrated because they are trying to get the client IP address in 3.5. So, here's some code which should work:

using System.ServiceModel;
using System.ServiceModel.Channels;

OperationContext context = OperationContext.Current;
MessageProperties prop = context.IncomingMessageProperties;
RemoteEndpointMessageProperty endpoint =
    prop[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
string ip = endpoint.Address;
Jader Dias
  • 88,211
  • 155
  • 421
  • 625
Paul Mrozowski
  • 6,604
  • 9
  • 34
  • 47
  • 11
    I couldn't edit the post, but it helped me a ton, thanks! Wanted to mention there are 2 errors. Should be "OperationContext" instead of "OperationContent" and should be "RemoteEndpointMessageProperty" instead of "RemoveEndpointMessageProperty". – Jeremy Mullin Oct 14 '09 at 04:19
  • 3
    Security note: This value can be spoofed... see MSDN – makerofthings7 Mar 07 '12 at 22:06
  • @makerofthings7 I see that on MSDN, but could it really be spoofed? The request still has a TCP handshake. If the IP was truly spoofed, wouldn't the syn ack get sent to the wrong place, and thus the connection would fail before it even began? – cost May 20 '13 at 09:02
  • 1
    @cost The "IP" in this case is not only in the TCP packet, but is also resident in the WCF message, however this text in the data stream (Layer 7) isn't properly secured' – makerofthings7 May 20 '13 at 12:05
  • @makerofthings7 If I understand you right, the IP isn't being retrieved from the TCP header, but rather from the WCF message itself? – cost May 20 '13 at 19:34
  • @cost Yes. Also, that is my understanding and experience when dealing with WCF services placed behind a NAT address. – makerofthings7 May 20 '13 at 20:27
  • @makerofthings7 Do you have some documentation to back that up, e.g. a specific header from which these fields are pulled? I find it very strange that this information would be duplicated higher up in the communications stack. – anton.burger Jun 20 '13 at 10:18
  • 1
    @shambulator It has been several years since I saw the issue, but the following KB article seems to indicate it may have been ports, not ipaddresses. http://support.microsoft.com/kb/971842 – makerofthings7 Jun 20 '13 at 13:42
  • what can you do if the first line when you get `OperationContext.Current` return null which is my problem currently – Franck Feb 18 '14 at 19:35
36

It turns out you can, so long as (a) your service is being hosted in a Web Service (obviously) and (b) you enable AspNetCompatibility mode, as follows:

    <system.serviceModel>
            <!-- this enables WCF services to access ASP.Net http context -->
            <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
...
    </system.serviceModel>

And then you can get the IP address by:

HttpContext.Current.Request.UserHostAddress
Martin Brown
  • 24,692
  • 14
  • 77
  • 122
Gaz
  • 4,064
  • 3
  • 32
  • 34
16

You can if you are targeting .NET 3.0 SP1.

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

Credits: http://blogs.msdn.com/phenning/archive/2007/08/08/remoteendpointmessageproperty-in-wcf-net-3-5.aspx

Reference: http://msdn.microsoft.com/en-us/library/system.servicemodel.channels.remoteendpointmessageproperty.aspx