Tolga Evcimen
Tolga Evcimen

Reputation: 7352

WCF how to detect if client and server are in same server

I have a service with multiple endpoints. These endpoints get requests from clients, and from each other too.

For the methods that gets the request from the other endpoints I need to make sure that the method can only be invoked from within the server.

I already have an authentication filter interception mechanism. I can bind this functionality to those certain methods. What I cannot figure out is how can I tell the request made from the same server. Take a look at below code snippet that I use for authentication:

public class ServiceUser_Authenticator : IParameterInspector
{
    public object BeforeCall ( string operationName, object[] inputs )
    {
        var ip = ( OperationContext.Current.IncomingMessageProperties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty ).Address;

        if ( ip != /* 127.0.0.1 , localhost , RealIP of the server */ )
            throw new FaultException("Access denied");

        return null;
    }
    ...
}

I am thinking to check if the ip of the client is same as mine, but don't know how. The RealIP(external) will probably work, but it better be a non-static value.

So, how can I check if the client of a wcf call is in the same server as wcf service?

Upvotes: 2

Views: 1112

Answers (1)

Emmanuel DURIN
Emmanuel DURIN

Reputation: 4913

In my humble opinion, the easiest and safest way to make some methods to be invoked only locally is to use NetNamedPipeBinding.

So I would take all the "local" methods and put them in a separate interface. And I would expose that interface with NetNamedPipeBinding.

Edit
You can expose different interfaces on the same service.
Each interface can have its own binding.

Edit 2 - code samples

In the two following samples, here is the service class exposing two interfaces

class ServiceHelloWorld : IPublicInterface, ILocalInterface

1. Many endpoints can be exposed through xml
These aren't the same interfaces. :

<services>
  <service name="HelloWorldService.ServiceHelloWorld">
    <endpoint address="net.tcp://localhost:7000/publicinterface" 
      binding="netTcpBinding" contract="IPublicInterface">
    <endpoint address="net.pipe://localhost:8000/privateinterface" 
      binding="netNamedBinding" contract="ILocalInterface">
  </service>
</services>

2. Many endpoints can be exposed through code

These aren't the same interfaces no more.

ServiceHost host =
   new ServiceHost(typeof(ServiceHelloWorld), new Uri[] { });
host.AddServiceEndpoint(typeof(IPublicInterface), 
   new NetTcpBinding(), "net.tcp://localhost:7000/publicinterface");
host.AddServiceEndpoint(typeof(ILocalInterface), 
   new NetNamedPipeBinding(), "net.pipe://localhost:8000/privateinterface");

Regards

Upvotes: 6

Related Questions