如何解决基于 WCF 证书的身份验证问题服务正在使用无效证书进行身份验证
我是 WCF 服务的初学者。尝试在 WCF 服务上实施基于证书的身份验证并面临问题。该服务需要来自调用客户端的特定证书。如果客户端没有传递任何证书,服务器会抛出身份验证错误。但与此同时,服务调用正在通过客户端提供的任何证书进行身份验证(如果客户端提供特定证书,服务假设进行身份验证)。
以下是服务器配置的代码片段:
服务配置:
<bindings>
<wsHttpBinding>
<binding name="MyWsHttpBinding" maxReceivedMessageSize="2147483647" receiveTimeout="00:30:00">
<readerQuotas maxStringContentLength="2147483647" maxBytesPerRead="2147483647" maxDepth="2147483647" maxArrayLength="2147483647"/>
<security mode="Transport">
<transport clientCredentialType="None" proxyCredentialType="None"/>
<message clientCredentialType="Certificate" algorithmSuite="Default"/>
</security>
</binding>
</wsHttpBinding>
</bindings>
<serviceBehaviors>
<behavior name="MyServiceBehavior">
<serviceCredentials>
<clientCertificate>
<authentication certificateValidationMode="ChainTrust" />
</clientCertificate>
<serviceCertificate findValue="e616ebcd940951794736624acc6484802018c8d4" storeLocation="LocalMachine" storeName="My" x509FindType="FindByThumbprint" />
</serviceCredentials>
<serviceMetadata httpsGetEnabled="true" httpGetEnabled="true"/>
<CustomBehaviorExtensionElement/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="MyEndpointBehavior">
<MySchemaValidator validateRequest="True" validateReply="False">
<schemas>
<add location="App_Data\model-service.xsd"/>
</schemas>
</MySchemaValidator>
</behavior>
</endpointBehaviors>
<services>
<service name="MyService" behaviorConfiguration="MyServiceBehavior">
<endpoint binding="wsHttpBinding" bindingConfiguration="MyWsHttpBinding" contract="MyExchangeService" behaviorConfiguration="MyEndpointBehavior" bindingNamespace="http://www.mycompany.com/exchange/"/>
<endpoint contract="IMetadataExchange" binding="mexHttpsBinding" address="mex" name="mex"/>
</service>
</services>
解决方法
问题的原因是您使用的安全模式是传输,因此只有以下代码有效:
<transport clientCredentialType="None" proxyCredentialType="None"/>
以下消息设置无效:
<message clientCredentialType="Certificate" algorithmSuite="Default"/>
将transport中的值改为certificate,也可以在官网下载wcf demo,有相关证书验证的例子,还有tutorials corresponding to the demo。
,我看到您的代码中使用的证书验证模式是 ChainTrust
。
<clientCertificate>
<authentication certificateValidationMode="ChainTrust" />
</clientCertificate>
如Microsoft Docs中所述,使用 ChainTrust 意味着 -
如果链建立到受信任的根存储中的证书颁发机构,则证书有效
意思是,客户端不需要发送与您的服务 web.config 中提到的指纹完全相同的证书。
事实上,任何在您的虚拟机的 Trusted Root Store
中存在根/中间证书颁发机构的证书都将通过验证。
要确保客户端能够仅使用特定证书对您的服务进行身份验证,请将 ChainTrust
更改为 PeerTrust
并将证书添加到 VM 的证书存储 (certmgr) 上的受信任人员存储中。
<authentication certificateValidationMode="PeerTrust" />
参考文献:
- MS Docs - Working with certificates in WCF
- Authentication element in web.config
- More info on Certificate Chain of Trust
如果觉得有用请点赞!谢谢:)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。