使用WSHttpBinding时如何限制对本地计算机的WCF服务的访问?

时间:2011-09-12 17:12:31

标签: .net wcf wshttpbinding

我正在使用WCF在我的机器上使用WSHttpBinding进行进程间通信,并且我想限制服务,因此只有当前机器上的进程才能调用该服务。我怎样才能做到这一点?

我更喜欢使用NetNamedPipesBinding,它固有地会执行此限制,但是在我的场景中这是不可能的,所以我想要一种使用WSHttpBinding来限制它的方法。我无法使用NetNamedPipesBinding的原因是该服务的一个客户端在低完整性进程(保护模式下的Internet Explorer)中运行,并且无权连接到更高完整性的命名管道(没有很多没有证件的jiggery-pokery like this看起来不错,但我宁愿避免。)

一种选择是添加一个IDispatchMessageInspector,它按照here所述的IP地址进行限制。这是最好的方法吗?

更新: 该软件将部署到数百台计算机上,因此使用证书等解决方案的工作量可能超出预期。

1 个答案:

答案 0 :(得分:0)

您可以尝试使用X509证书来创建安全签名。这样,您可以按住锁和钥匙。其他IP可以打你的服务,但不能沟通。你可以这样做:

在服务中:

          <behaviors>
  <serviceBehaviors>
    <behavior name="wsHttpCertificateBehavior">
      <dataContractSerializer maxItemsInObjectGraph="50000"/>
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="false" />
      <serviceCredentials>
        <clientCertificate>
          <authentication certificateValidationMode="ChainTrust" revocationMode="NoCheck" />
        </clientCertificate>
        <serviceCertificate findValue="CN=WSE2QuickStartServer" storeLocation="LocalMachine"
          storeName="My" x509FindType="FindBySubjectDistinguishedName" />
      </serviceCredentials>
    </behavior>
    <behavior name="">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="false" />
    </behavior>
  </serviceBehaviors>
</behaviors>

                 

在客户端:

    <behaviors>
  <endpointBehaviors>
    <behavior name="wsHttpCertificateBehavior">
      <dataContractSerializer maxItemsInObjectGraph="50000" />
      <clientCredentials>
        <clientCertificate findValue="CN=WSE2QuickStartClient" storeLocation="CurrentUser" storeName="My" x509FindType="FindBySubjectDistinguishedName" />
        <serviceCertificate>
          <authentication certificateValidationMode="ChainTrust" revocationMode="NoCheck" trustedStoreLocation="LocalMachine" />
        </serviceCertificate>
      </clientCredentials>
    </behavior>
  </endpointBehaviors>
</behaviors>

<client>
  <endpoint address="https://localhost/ClientService.svc" behaviorConfiguration="wsHttpCertificateBehavior" binding="wsHttpBinding" bindingConfiguration="ApplicationServicesBinding" contract="GAINABSApplicationServices.Contracts.ServiceContracts.IClientService" name="ClientService">
    <!--<identity>
      <certificateReference storeName="AddressBook" storeLocation="CurrentUser"
        x509FindType="FindBySubjectName" findValue="WSE2QuickStartServer"
        isChainIncluded="true" />
    </identity>-->
  </endpoint>
 </client>

您可以选择在客户端上使用Identity标记来声明在与服务通信时明确使用身份证书。希望这有帮助!