首页 > 解决方案 > 为什么在使用 WCF 服务时客户端应用程序需要证书的私钥?

问题描述

设想

X.509 证书:组织提供的证书用于服务器-客户端身份验证。该证书同时用于服务器和客户端身份验证。

WCF 服务: WCF 服务托管在 IIS 中并在开发服务器上运行。它由 X.509 pfx 证书(由组织提供,非自签名)保护。它在其配置中使用消息安全性。.Pfx证书已通过 mmc 在本地计算机上的个人-> 证书和受信任的根证书颁发机构-> 证书中成功导入到开发服务器上

客户端:它使用与服务相同的绑定结构。该.cer文件(在开发服务器上使用的相同证书)在我的本地导入到个人-> 证书和受信任的根证书颁发机构-> 证书中。

服务配置:

<system.serviceModel>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" 
            minFreeMemoryPercentageToActivateService="0" />
    <services>
        <service name="WCF_HOST.Service1" behaviorConfiguration="customBehaviour">
            <host>
                <baseAddresses>
                    <add baseAddress = "http://localhost:57104/Service1" />
                    <!--<add baseAddress = "http://****:57104/Service1" />-->
               </baseAddresses>
            </host>
            <endpoint
                address=""
                binding="wsHttpBinding"
                bindingConfiguration="customWsHttpBinding"
                contract="WCF_HOST.IService1">
            </endpoint>
            <endpoint 
                address="mex" binding="mexHttpBinding" 
                contract="IMetadataExchange"/>
        </service>
    </services>
    <bindings>
        <wsHttpBinding>
            <binding name="customWsHttpBinding">
                <security mode="Message">
                    <message clientCredentialType="Certificate"/>
                </security>
                <!--<readerQuotas
                       maxDepth="2147483647"
                       maxStringContentLength="2147483647"
                       maxArrayLength="2147483647"
                       maxBytesPerRead="2147483647"
                       maxNameTableCharCount="2147483647" />-->
            </binding>
        </wsHttpBinding>
    </bindings>
    <behaviors>
        <serviceBehaviors>
            <behavior name="customBehaviour">
                <serviceMetadata httpGetEnabled="True"/>
                <serviceDebug includeExceptionDetailInFaults="False" />
                <serviceCredentials>
                     <serviceCertificate findValue="tempCert"
                             storeLocation="LocalMachine" storeName="My"
                             x509FindType="FindBySubjectName" />
                     <clientCertificate>
                         <authentication certificateValidationMode="PeerOrChainTrust" 
                             revocationMode="NoCheck" />
                     </clientCertificate>
                </serviceCredentials>
            </behavior>
        </serviceBehaviors>
    </behaviors>
</system.serviceModel>

客户端配置:

<system.serviceModel>
    <bindings>
      <wsHttpBinding>
        <binding name="customWsHttpBinding">
          <security mode="Message">
            <message clientCredentialType="Certificate" negotiateServiceCredential="true"/>
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>
    <client>
      <endpoint 
        address="http://****:57104/Service1.svc" 
        binding="wsHttpBinding" 
        bindingConfiguration="customWsHttpBinding" 
        behaviorConfiguration="customBehavior" 
        contract="ServiceClient.IService1">
        <identity>
          <dns value="tempCert"/>
        </identity>
      </endpoint>
    </client>
    <behaviors>
      <endpointBehaviors>
        <behavior name="customBehavior">
          <clientCredentials>
            <serviceCertificate>
              <defaultCertificate findValue="he f5 l9 f9 41 e1 f5 ea 20 ef 55 bc 99 4c ca 5b c4 c5 31 d9" storeLocation="LocalMachine" storeName="My" x509FindType="FindByThumbprint"/>
              <authentication certificateValidationMode="PeerOrChainTrust" revocationMode="NoCheck"/>
            </serviceCertificate>
            <clientCertificate 
              findValue="tempCert"
              storeLocation="CurrentUser" 
              storeName="My" x509FindType="FindBySubjectName"/>
          </clientCredentials>
        </behavior>
      </endpointBehaviors>
    </behaviors>
  </system.serviceModel>

证书说明

在此处输入图像描述

问题

  1. 当我尝试在webmethod调用时使用服务时,它会引发异常

    证书“CN=tempCert, OU=***19, O=****Inc., C=SG”必须有私钥。该进程必须具有私钥的访问权限

    为什么客户端应用程序在这里需要一个私钥?

  2. 如果我向.pfx客户端应用程序提供相同的证书(在服务器上使用),它就可以工作。如何以及为什么?

  3. 在这个问题上浪费了无数个小时,非常感谢任何帮助/建议。

标签: .netwcfcertificatex509certificate

解决方案


推荐阅读