简体   繁体   English

WCF如何检测客户端和服务器是否在同一服务器中

[英]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. 我正在考虑检查客户端的IP是否与我的相同,但不知道如何。 The RealIP(external) will probably work, but it better be a non-static value. RealIP(external)可能会起作用,但最好是一个非静态值。

So, how can I check if the client of a wcf call is in the same server as wcf service? 因此,如何检查wcf呼叫的客户端是否与wcf服务在同一服务器中?

In my humble opinion, the easiest and safest way to make some methods to be invoked only locally is to use NetNamedPipeBinding . 以我的拙见,使某些方法仅在本地调用的最简单,最安全的方法是使用NetNamedPipeBinding

So I would take all the "local" methods and put them in a separate interface. 因此,我将采用所有“本地”方法并将它们放在单独的接口中。 And I would expose that interface with NetNamedPipeBinding . 我将使用NetNamedPipeBinding公开该接口。

Edit 编辑
You can expose different interfaces on the same service . 您可以在同一服务上公开不同的接口。
Each interface can have its own binding. 每个接口可以具有自己的绑定。

Edit 2 - code samples 编辑2-代码示例

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 1.许多端点可以通过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 2.许多端点可以通过代码公开

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 问候

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM