我有一个嵌入到 Windows 服务中的 WCF 服务。它绑定(bind)到本地主机,但它也接受来自这种 URL 的连接 - “http://ip:port/ServiceName”,我怎样才能对其他人隐藏它并只允许来自本地主机的连接。
这是我的服务配置
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="Test.Service.ServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="Test.Service.ServiceBehavior" name="Test.Service.TestService">
<endpoint address="localhost" binding="wsHttpBinding" contract="Test.Service.IService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8732/MyService/service" />
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
最佳答案
要“隐藏”它,您需要关闭任何元数据交换,因此您需要删除:
<serviceMetadata httpGetEnabled="true" />
从您的服务行为中,您需要删除 mex 端点:
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
然而,这只是“模糊”了它。为了避免除 localhost 之外的任何其他人调用 - 为什么不切换到 netNamedPipeBinding
,它是“仅在这台机器上”设计的 - 外部调用者无法调用该端点。
否则,您必须检查调用者的 IP 地址并根据该信息阻止他们 - 但是很容易被欺骗....
关于c# - 如何隐藏我的 WCF 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4265968/