【问题标题】:Client certificate authentication WCF客户端证书身份验证 WCF
【发布时间】:2013-10-07 18:34:50
【问题描述】:

所以我完全迷失了证书。我已经在网上搜索了解决方案和教程,但没有发现任何可以真正帮助我的东西。 我要做的是对我的 WCF 客户端-服务器应用程序进行服务器和客户端证书验证。该应用程序托管在 IIS 上。 我希望它在我的开发计算机(服务器是本地主机)和测试中(我的客户端和服务器是 Windows 服务器)。

我现在的配置是:

客户:

<behaviors>
  <endpointBehaviors>
    <behavior name="myBehaviorConfig">
      <clientCredentials>
        <clientCertificate findValue="CN=MyTestClientCertificate"
                            storeLocation="LocalMachine"
                            x509FindType="FindBySubjectDistinguishedName"/>
      </clientCredentials>
    </behavior>
  </endpointBehaviors>
</behaviors>

<bindings>
  <wsHttpBinding>
    <binding name="MyBindingConfig">
      <security mode="TransportWithMessageCredential">
        <transport realm=""/>
        <message clientCredentialType="Certificate"/>
      </security>
    </binding>
  </wsHttpBinding>
</bindings>

<client>
  <endpoint address="https://localhost/Service.Calculator.svc"
            binding="wsHttpBinding"
            bindingConfiguration="MyBindingConfig"
            behaviorConfiguration="MyBehaviorConfig"
            contract="Service.ICalculator"
            name="ICalculatorServiceEndpoint">
    <identity>
      <servicePrincipalName value="host"/>
    </identity>
  </endpoint>    
</client>

服务器:

 <behaviors>
  <serviceBehaviors>
    <behavior name="myBehavior">
      <serviceCredentials>
        <serviceCertificate findValue="CN=MyTestRootCA"
                            storeLocation="LocalMachine"
                            x509FindType="FindBySubjectDistinguishedName"/>
        <userNameAuthentication userNamePasswordValidationMode="Windows"/>
        <clientCertificate>
          <authentication certificateValidationMode="PeerOrChainTrust"/>
        </clientCertificate>
      </serviceCredentials>
      <serviceMetadata httpsGetEnabled="true"/>
      <serviceDebug includeExceptionDetailInFaults="true"/>
      <unity operationContextEnabled="true"
             instanceContextEnabled="true"
             contextChannelEnabled="true"
             serviceHostBaseEnabled="true"/>
    </behavior>
  </serviceBehaviors>
</behaviors>

<bindings>
  <wsHttpBinding>
    <binding name="MyBinding">
      <security mode="TransportWithMessageCredential">
        <transport realm=""/>
        <message clientCredentialType="Certificate"/>
      </security>
    </binding>
  </wsHttpBinding>
</bindings>

<services>
  <service name="Service.Calculator"
           behaviorConfiguration="myBehavior">
    <endpoint address=""
              binding="wsHttpBinding"
              bindingConfiguration="MyBinding"
              contract="Service.ICalculator" />
    <endpoint address="mex"
              binding="mexHttpsBinding"
              contract="IMetadataExchange"
              name="CalculatorServiceMex" />
  </service>
</services>

“CN=MyTestRootCA”是“Authority”的“Creating”证书,我把他放在本地计算机上的受信任根证书以及本地计算机的个人目录中。 并且是客户端证书“CN=MyTestClientCertificate”的颁发者。

几件事..

我知道客户端证书应该在“MMC”的 CurretUser 目录中,但是当它在那里时,我有一个例外,即应用程序找不到证书。 我尝试通过“FindBySubjectDistinguishedName”和“FindByThumbprint”定位它,两次都是相同的例外“无法找到具有给定标准的证书......”所以我把它放在 LocalMachine 中并且很好。 有人知道为什么它不起作用吗?

我有很多问题和例外=\当前的一个是: “X.509 证书中未提供私钥” 有谁熟悉这个异常并知道如何解决它?

非常感谢您的回答,我已经坐了几天了

【问题讨论】:

    标签: wcf iis certificate client-certificates


    【解决方案1】:

    您的配置文件未指定 clientCertificate storeLocation 值,因此客户端证书需要位于 LocalMachine 存储中,这是 storeLocation 的 默认 值。

    考虑以下来自msdn 的示例,它设置客户端证书存储位置:

    <clientCertificate>
       <certificate 
             findValue="www.cohowinery.com" 
             storeLocation="CurrentUser" 
             storeName="TrustedPeople"
             x509FindType="FindByIssuerName" />
       <authentication …
    

    注意:另一个错误“X.509 证书中未提供私钥”很可能是因为您的证书没有关联的私钥或您的进程的用户上下文没有访问权限而引发的私钥。

    【讨论】:

    • 我确实在客户端配置中指定了客户端证书的存储位置。您建议的代码应该在服务器配置中?如果是这样,我应该指定什么证书,客户端证书位于客户端的计算机上。关于第二个错误,我实际上确实有一个私钥,我用它来创建客户端证书,但仅此而已。我需要用它做其他事情吗?喜欢将其放在特定位置或将其添加到配置中?
    猜你喜欢
    • 2011-04-09
    • 2018-02-12
    • 1970-01-01
    • 2012-01-03
    • 1970-01-01
    • 2022-06-23
    • 1970-01-01
    • 1970-01-01
    • 2012-09-01
    相关资源
    最近更新 更多