[英]WCF how to detect if client and server are in same server
我有多個端點的服務。 這些端點從客戶端以及彼此之間獲取請求。
對於從其他端點獲取請求的方法,我需要確保只能從服務器內部調用該方法。
我已經有一個身份驗證篩選器攔截機制。 我可以將此功能綁定到某些方法。 我無法確定的是如何分辨同一台服務器發出的請求。 看一下我用於身份驗證的以下代碼片段:
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;
}
...
}
我正在考慮檢查客戶端的IP是否與我的相同,但不知道如何。 RealIP(external)
可能會起作用,但最好是一個非靜態值。
因此,如何檢查wcf呼叫的客戶端是否與wcf服務在同一服務器中?
以我的拙見,使某些方法僅在本地調用的最簡單,最安全的方法是使用NetNamedPipeBinding
。
因此,我將采用所有“本地”方法並將它們放在單獨的接口中。 我將使用NetNamedPipeBinding
公開該接口。
編輯
您可以在同一服務上公開不同的接口。
每個接口可以具有自己的綁定。
編輯2-代碼示例
在以下兩個示例中,這是暴露兩個接口的服務類
class ServiceHelloWorld : IPublicInterface, ILocalInterface
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>
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");
問候
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.