0

I'm working on C# WCF, I have to adapt a piece of code.

The base code contains :

public override void OnActionExecuting(HttpActionContext actionContext)
{
    string ipAddress = HttpContext.Current.Request.UserHostAddress;
    WinEventLog.logInfo("Connection from : " + ipAddress);
    bool test = IsIpAddressAllowed(ipAddress.Trim());
    [..]
}

private bool IsIpAddressAllowed(string IpAddress)
{
    [..]
}

But in WCF, i'm unable to get the HttpContext.Current.Request.UserHostAddress.

My WCF Code is :

[ServiceContract]
[RequiredParametersBehavior]
public interface IMyService
{
    [OperationContract]
    String Request(String environment, String request);
}

How can I get the user IP address in my function Request ?

A.Pissicat
  • 3,023
  • 4
  • 38
  • 93

1 Answers1

0

I found a solution using OperationContext.

There is my method :

private bool IsIpAddressAllowed(string IpAddress)
{
    MessageProperties prop = context.IncomingMessageProperties;
    RemoteEndpointMessageProperty endpoint =
        prop[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;

    String ipAddress = endpoint.Address; // Here I get the IP

    WinEventLog.EventLog.logInfo(null, "Connection from : " + ipAddress);

    return MyTestOnIP(ipAddress);
}

Called like this :

bool isOK = IsIpAddressAllowed(System.ServiceModel.OperationContext.Current);
A.Pissicat
  • 3,023
  • 4
  • 38
  • 93