我有一个有多个端点的服务.这些端点从客户端获取请求,也从彼此获取请求.
对于从其他端点获取请求的方法,我需要确保只能从服务器内调用该方法.
我已经有了一个身份验证过滤器拦截机制.我可以将此功能绑定到那些特定方法.我无法弄清楚的是如何判断来自同一服务器的请求.看一下我用于身份验证的下面的代码片段:
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;
}
...
}
Run Code Online (Sandbox Code Playgroud)
我正在考虑检查客户端的ip是否与我的相同,但不知道如何.这RealIP(external)可能会起作用,但最好是非静态值.
那么,我如何检查wcf调用的客户端是否与wcf服务在同一服务器中?
在我看来,使用本地调用某些方法的最简单,最安全的方法就是使用NetNamedPipeBinding.
所以我会采用所有"本地"方法并将它们放在一个单独的界面中.我会公开那个接口 NetNamedPipeBinding.
编辑
您可以在同一服务上公开不同的接口.
每个接口都可以有自己的绑定.
编辑2 - 代码示例
在以下两个示例中,这里是公开两个接口的服务类
class ServiceHelloWorld : IPublicInterface, ILocalInterface
Run Code Online (Sandbox Code Playgroud)
1.许多端点可以通过xml公开.
这些接口不是相同的.:
<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>
Run Code Online (Sandbox Code Playgroud)
2.许多端点可以通过代码公开
这些不再是相同的接口.
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");
Run Code Online (Sandbox Code Playgroud)
问候