我有一个使用此类行为的 WCF 服务:
<behavior name="WCFServiceCertificate.Service1Behavior">
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
<serviceCredentials>
<clientCertificate>
<authentication certificateValidationMode="ChainTrust"/>
</clientCertificate>
<serviceCertificate findValue="localhost" storeLocation="LocalMachine" storeName="My" x509FindType="FindBySubjectName"/>
</serviceCredentials>
</behavior>
它使用我通过 makecert 创建的名为“localhost”的证书。首先,我创建了根证书颁发机构,然后创建了一个证书。我还生成了一个保存在文件中的客户端证书。
然后,我有一个使用该 Web 服务的客户端应用程序。 App.config 包括:
<bindings>
<wsHttpBinding>
<binding name="WSHttpBinding_IWS">
<security>
<message clientCredentialType="Certificate" />
</security>
</binding>
</wsHttpBinding>
</bindings>
然后我从文件中为客户端加载证书:
X509Certificate2 client = new X509Certificate2("client.pfx", "pass");
所有证书的东西似乎都正确完成,但是当我想从客户端调用任何服务方法时,它说:
The caller was not authenticated by the service
有人可以给我一些关于如何将 SOAP header 中的证书从客户端正确传递到服务器的建议吗?我想念什么?
最佳答案
使用证书不是一件容易的事,特别是在 WCF 中调试它。因此,您可以考虑采取以下步骤来缩小问题范围。
嵌入证书后,您的服务是否运行?测试它是否正常工作的一种方法是尝试在浏览器中访问您的服务。
您访问客户端中的服务地址是否与 CN=localhost 证书中描述的相同?
例。 http://location/Path/Service.svc
不是这个:
例。 http://computername/Path/Service.svc
或者
例。 http://computername.domain.com/Path/Service.svc
解释:
如果证书的 Common Name 中的名称与颁发给该证书的 URL 名称不同,则证书将无效。
你们在同一台机器上吗? CN=localhost 的证书只能在同一台机器上使用。
由于您在 IIS 中托管您的服务,因此您需要配置 IIS 以使用您创建的证书并将其绑定(bind)到端口 443。
如何?
- 转到您的 IIS 和客户端默认网站
- 点击右侧的绑定(bind)位置
- 如果协议(protocol) HTTPS 已经配置,请尝试编辑它并分配您的证书。如果不是,请使用端口 443 添加协议(protocol) HTTPS 并分配您的证书。如果您在商店中正确安装,证书应该会显示在列表中。
通过这些步骤,我们可以知道从哪里开始。可能是服务问题、客户端问题或 IIS 配置问题。希望这能帮助我们解决您的问题。
关于c# - 将 X.509 证书发送到 WCF c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31824490/