wcf - 使用 SSL 和证书为 WCF 设置配置文件

标签 wcf web-services binding ssl wcf-binding

我正在尝试使用 SSL 在 IIS 7 中设置 WCF 网络服务,但我对配置文件有点迷茫。 我希望从服务器到客户端的数据是乱码(SSL 就够了吗?) 客户端还需要通过证书向服务器表明自己的身份。

我有以下证书:

  • dev.test.com - 访问 url https://dev.test.com/TestService.svc表明存在此有效证书。
  • TestServer - 标识服务器的虚拟证书(我真的需要这个吗?或者我可以重用 dev.test.com?也许有 server.test.com?)
  • TestClient - 客户端的虚拟证书

我的配置文件是这样设置的:

Web.config(服务器):

<bindings>
  <wsHttpBinding>
    <binding name="wsHttpEndpointBinding"
             messageEncoding="Mtom">
      <security mode="Message">
        <transport clientCredentialType="None" />
        <message clientCredentialType="Certificate" />
      </security>
    </binding>
  </wsHttpBinding>
</bindings>

<services>
  <service
    name="TestService"
    behaviorConfiguration="TestServiceBehavior">
    <endpoint
      name="TestEndPoint"
      address=""
      binding="wsHttpBinding"
      bindingConfiguration="wsHttpEndpointBinding"
      bindingNamespace="http://www.example.com/"
      contract="iWebService">
      <!--<identity>
        <dns value=""/>
      </identity>-->
    </endpoint>
    <endpoint address="mex" binding="mexHttpsBinding"  bindingConfiguration="" name="MexHttpsBindingEndpoint" contract="IMetadataExchange"/>
  </service>
</services>

<behaviors>
  <serviceBehaviors>
    <behavior name="TestServiceBehavior">
      <serviceMetadata httpGetEnabled="false" httpsGetEnabled="true" />
      <serviceCredentials>
        <clientCertificate>
          <authentication certificateValidationMode="PeerOrChainTrust" />
        </clientCertificate>
        <serviceCertificate findValue="TestServer" storeLocation="LocalMachine"
          storeName="My" x509FindType="FindBySubjectName" />
      </serviceCredentials>
    </behavior>
  </serviceBehaviors>
</behaviors>

<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />

App.config(客户端):

<bindings>
  <wsHttpBinding>
    <binding name="wsHttpBinding" bypassProxyOnLocal="false"
      transactionFlow="false" hostNameComparisonMode="StrongWildcard"
      messageEncoding="Mtom" textEncoding="utf-8" useDefaultWebProxy="true"
      allowCookies="false">
      <reliableSession ordered="true"
        enabled="false" />
      <security mode="Message">
        <transport clientCredentialType="None" proxyCredentialType="None"
          realm="" />
        <message clientCredentialType="Certificate" negotiateServiceCredential="true"
          algorithmSuite="Default" />
      </security>
    </binding>
    <binding name="TestEndPoint" bypassProxyOnLocal="false"
      transactionFlow="false" hostNameComparisonMode="StrongWildcard"
      messageEncoding="Mtom"
      textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false">
      <reliableSession ordered="true"
        enabled="false" />
      <security mode="Transport">
        <transport clientCredentialType="None" proxyCredentialType="None"
          realm="" />
        <message clientCredentialType="Certificate" negotiateServiceCredential="true" />
      </security>
    </binding>
  </wsHttpBinding>
</bindings>

<client>
  <endpoint address="https://dev.test.com/TestService.svc"
    behaviorConfiguration="TestServiceBehavior"
    binding="wsHttpBinding" bindingConfiguration="wsHttpBinding"
    contract="IContractName" name="wsHttpBinding">
    <identity>
      <dns value="TestServer" />
    </identity>
  </endpoint>
  <endpoint address="https://dev.test.com/DistributionCenterService.svc"
    binding="wsHttpBinding" bindingConfiguration="TestEndPoint" contract="IContract.Name"
    name="TestEndPoint" />
</client>

<behaviors>
  <endpointBehaviors>
    <behavior name="TestServiceBehavior">
      <clientCredentials>
        <clientCertificate findValue="TestClient"
                           storeName="My"
                           storeLocation="CurrentUser"
                           x509FindType="FindBySubjectName"/>
        <serviceCertificate>
          <authentication
            certificateValidationMode="PeerOrChainTrust"
            revocationMode="NoCheck"
            trustedStoreLocation="CurrentUser"/>
        </serviceCertificate>
      </clientCredentials>
    </behavior>
  </endpointBehaviors>
</behaviors>

当我尝试访问 https://dev.test.com/TestService.svc , 我得到
找不到与具有绑定(bind) WSHttpBinding 的终结点的方案 http 相匹配的基址。注册基址方案为 [https]。

无论如何,我真的不知道应该使用什么配置设置。

最佳答案

我相信您要能够使用 https,客户端 wsHttpBinding 上的安全模式需要是 Transport 或(可能在您的情况下)TransportWithMessageCredential。

关于wcf - 使用 SSL 和证书为 WCF 设置配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10639897/

相关文章:

wcf - 如何让 WCF 网站保持在线?

asp.net-mvc - 使用用户身份从移动客户端访问 WCF 服务

NServiceBus 主机问题中的 WCF 服务托管

c# - 摆脱 WS 安全 header 中的 Expires 节点 (xml)

Angular 2 - 将纯文本传递给输入

WPF - 绑定(bind)和渲染转换

wcf - WCF 中的 MaxReceivedMessageSize 错误

web-services - 多个请求的服务持久化

java - 如何通过非 Spring 配置文件配置 Apache CXF 以使用单个 JAXBContext?

wpf - 找不到与引用绑定(bind)的源... databound ListView 问题