Get Client IP address using WCF 4.5 RemoteEndpointMessageProperty in load balancing situation

RGS picture RGS · Oct 16, 2015 · Viewed 18.2k times · Source

I have hosted WCF 4.5 Restful service in IIS and I am trying to use RemoteEndpointMessageProperty to get the IP address of the client who consumes the service.

Code 1:

private string GetClientIP()
{
  OperationContext context = OperationContext.Current;
  MessageProperties prop = context.IncomingMessageProperties;
  RemoteEndpointMessageProperty endpoint =
         prop[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
  string ip = endpoint.Address;
  return ip;
}

Code 2:

private string GetClientIP()
{
  string retIp = string.Empty;
  OperationContext context = OperationContext.Current;
  MessageProperties prop = context.IncomingMessageProperties;
  HttpRequestMessageProperty endpointLoadBalancer =
  prop[HttpRequestMessageProperty.Name] as HttpRequestMessageProperty;
  if (endpointLoadBalancer.Headers["X-Forwarded-For"] != null)
  {
    retIp = endpointLoadBalancer.Headers["X-Forwarded-For"];
  }
  if (string.IsNullOrEmpty(retIp))
  {
    RemoteEndpointMessageProperty endpoint =
                prop[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
                retIp = endpoint.Address;
  }
  return retIp;
}

However, since the WCF service is hosted in IIS behind a load balancer, the IP address I got is always the IP of the load balancer. Is there any way to get around this so that I can get the true IP of the client?

Answer

Mahesh Malpani picture Mahesh Malpani · May 11, 2016
OperationContext context = OperationContext.Current;
MessageProperties properties = context.IncomingMessageProperties;
RemoteEndpointMessageProperty endpoint = properties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
string address = string.Empty;
//http://www.simosh.com/article/ddbggghj-get-client-ip-address-using-wcf-4-5-remoteendpointmessageproperty-in-load-balanc.html
if (properties.Keys.Contains(HttpRequestMessageProperty.Name))
{
    HttpRequestMessageProperty endpointLoadBalancer = properties[HttpRequestMessageProperty.Name] as HttpRequestMessageProperty;
    if (endpointLoadBalancer != null && endpointLoadBalancer.Headers["X-Forwarded-For"] != null)
        address = endpointLoadBalancer.Headers["X-Forwarded-For"];
}
if (string.IsNullOrEmpty(address))
{
    address = endpoint.Address;
}

This works in case of load balancer and without it also. I had one endpoint as TCP and other one as web http for REST API.