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

时间:2012-05-17 16:43:42

标签: wcf web-services binding ssl wcf-binding

我正在尝试使用SSL在IIS 7中设置WCF Web服务,但我对配置文件有点失落。 我希望从服务器到客户端的数据都是乱码(这对SSL来说够了吗?) 客户端还需要通过证书将自己标识为服务器。

我有以下证书:

  • dev.test.com - 访问网址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时,我得到了 Could not find a base address that matches scheme http for the endpoint with binding WSHttpBinding. Registered base address schemes are [https].

无论如何,我真的迷失了我应该使用的配置设置。

1 个答案:

答案 0 :(得分:1)

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