首页 > 解决方案 > 如何在不创建新客户端或破坏现有客户端的情况下制作只有 HTTP 和 HTTPS 的 WCF 服务

问题描述

我有一个使用 basicHttpBinding 的 WCF 服务。它作为 HTTP 公开给许多客户端。

现在我们的要求是将此服务作为 HTTPS 公开给少数新客户端,同时我们不应该破坏通过 http 消费的现有客户端。

我遇到了以编程方式检测安全类型然后公开,但是还有其他更好更简单的方法吗?只是使用端点配置等?

我对这些领域还很陌生,请举个例子

标签: c#.netwcfhttpswcf-security

解决方案


是的,正如您在两天前为我们的一些服务所做的配置中提到的那样,有一个更好的解决方案。

添加basicHttpBinding如下:

<basicHttpBinding>
  <binding allowCookies="true" openTimeout="00:00:10" maxReceivedMessageSize="20000000">
    <readerQuotas maxDepth="32" maxArrayLength="200000000" maxStringContentLength="200000000" />
    <security mode="None" />
  </binding>
  <binding name="secureBasicHttpBinding">
    <security mode="Transport">
      <transport clientCredentialType="None"/>
    </security>
  </binding>
</basicHttpBinding>

和服务端点:

<service  name="ServiceName">
  <endpoint address="rest" behaviorConfiguration="Rest.Behavior" binding="webHttpBinding" contract="System.Data.Services.IRequestHandler">
    <identity>
      <dns value="localhost"/>
    </identity>
  </endpoint>
  <endpoint address="rest" behaviorConfiguration="Rest.Behavior" binding="webHttpBinding" bindingConfiguration="secureBasicHttpBinding" contract="your service contract">
    <identity>
      <dns value="localhost" />
    </identity>
  </endpoint>
  <host>
    <baseAddresses>
      <add baseAddress="http://localhost/Service.svc"/>
    </baseAddresses>
  </host>
</service>

在 IIS 中,您只需要添加有效证书并启用它,https使用secureBasicHttpBindinghttp使用默认的 basic httpConfiguration

我之前已经测试过它,现在一些客户在使用该服务,而https其他一些客户正在使用http.

小费:

在本地模式下,通过上述配置托管服务时出现错误,WCF所以我得出这个结论,将该配置置于release模式而不是debug模式,因为https在工作服务器上启用了该配置。

所以在release配置中有类似的东西(发布后转移):

<service  name="ServiceName" xdt:Locator="Match(name)" xdt:Transform="Replace">
  <endpoint address="rest" behaviorConfiguration="Rest.Behavior" binding="webHttpBinding" contract="System.Data.Services.IRequestHandler">
    <identity>
      <dns value="localhost"/>
    </identity>
  </endpoint>
  <endpoint address="rest" behaviorConfiguration="Rest.Behavior" binding="webHttpBinding" bindingConfiguration="secureBasicHttpBinding" contract="your service contract">
    <identity>
      <dns value="localhost" />
    </identity>
  </endpoint>
  <host>
    <baseAddresses>
      <add baseAddress="http://localhost/AAA/Service.svc"/>
    </baseAddresses>
  </host>
</service>

推荐阅读