如果您想使用 x509 证书对调用 WCF 服务的客户端进行身份验证,那么您参考的第一篇文章是一个非常好的分步指南。
http://www.codeproject.com/Articles/36683/simple-steps-to-enable-X-certificates-on-WCF
本文假设您的客户端和服务都在同一台机器上运行,因此您需要根据具体情况调整说明。
第 1 步:创建客户端和服务器证书
由于您已经拥有证书,您可以跳过此步骤。您必须在步骤 3 和 7 中将 WcfServer 和 WcfClient 替换为您的服务器和客户端证书中的名称。
第 2 步:复制受信任的人证书中的证书
引用的文章解释了如何在 MMC 中打开证书管理单元。由于您使用的是现有证书,因此您可能需要在服务器上导入服务证书,在客户端计算机上导入客户端证书。然后导出没有私钥的客户端证书并将其导入服务器计算机上的 Trusted People 文件夹中。
步骤 3:在 WCF 服务 web.config 文件中指定证书
<serviceCredentials>
<clientCertificate>
<authentication certificateValidationMode="PeerTrust"/>
</clientCertificate>
<serviceCertificate findValue="!Insert name from your server certificate here!"
storeLocation="LocalMachine"
storeName="My"
x509FindType="FindBySubjectName" />
第 4 步:在 WCF 服务 web.config 文件中定义绑定
<bindings>
<wsHttpBinding name="wsHttpEndpointBinding">
<binding>
<security>
<message clientCredentialType="Certificate" />
</security>
</binding>
</wsHttpBinding>
</bindings>
第 5 步:将绑定与 WCF 服务 web.config 文件中的端点绑定
<endpoint address="" binding="wsHttpBinding"
bindingConfiguration="wsHttpEndpointBinding" contract="WCFServiceCertificate.IService1">
第 6 步:制作用于使用 WCF 服务的 Web 应用程序客户端。
添加服务引用时,Visual Studio 会在客户端 web.config 文件中插入元素。您需要按照第 7 步和第 8 步进行修改。
步骤 7:在客户端 web.config 文件中为 WCF 客户端定义证书
<behaviors>
<endpointBehaviors>
<behavior name="CustomBehavior">
<clientCredentials>
<clientCertificate findValue="!Insert name from your client certificate here!"
x509FindType="FindBySubjectName"
storeLocation="CurrentUser" storeName="My" />
<serviceCertificate>
<authentication certificateValidationMode="PeerTrust"/>
</serviceCertificate>
</clientCredentials>
</behavior>
</endpointBehaviors>
第 8 步:在客户端 web.config 文件中将行为与 WCF 客户端上的端点绑定
您需要替换地址、合同和 dns 值以匹配您的实际 WCF 服务。
<client>
<endpoint address="http://localhost:1387/Service1.svc" binding="wsHttpBinding"
bindingConfiguration="WSHttpBinding_IService1" contract="ServiceReference1.IService1"
name="WSHttpBinding_IService1" behaviorConfiguration="CustomBehavior">
<identity>
<dns value="WcfServer" />
</identity>
</endpoint>
</client>