如何在不创建新客户端或破坏现有客户端的情况下使WCF服务既是HTTP又是HTTPS

时间:2018-11-21 11:00:17

标签: c# .net wcf https wcf-security

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

现在,我们的要求是向少数新客户端公开此服务作为HTTPS,而我们不应破坏通过http进行消费的现有客户端。

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

我在这些领域还很陌生,请提供示例帮助

1 个答案:

答案 0 :(得分:0)

是的,有一个更好的解决方案,正如您提到的,我两天前就为我们的某些服务进行了配置。

添加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使用secureBasicHttpBinding,而http使用默认的基本httpConfiguration

我之前已经测试过它,现在有一些客户端在https中使用该服务,而另一些客户端正在使用http

提示:

在本地模式下,通过上述配置托管WCF服务时出错,因此我得出以下结论,将该配置置于release模式而不是debug模式,因为{{ 1}}在工作服务器上启用。

因此在https的配置中有类似的东西(发布后将被传输):

release