gpt4 book ai didi

wcf - 通过 HTTPS 上的客户端证书对 WCF 请求进行身份验证

转载 作者:太空宇宙 更新时间:2023-11-03 12:41:36 25 4
gpt4 key购买 nike

在过去的一周里,我一直在为这个糟糕的 WCF 服务的配置而苦苦挣扎,而且我正在放慢速度,开始怀疑我尝试做的事情是不可能的,尽管有文档。

很简单,我想让 WCF 服务需要客户端证书(服务器将在其证书存储区中拥有该证书),然后使用 System.ServiceModel.ServiceSecurityContext 访问该身份。此外,这需要使用传输安全。

这是我的服务器配置:

<system.serviceModel>
<services>
<service behaviorConfiguration="requireCertificate" name="Server.CXPClient">
<endpoint address="" binding="wsHttpBinding" bindingConfiguration="wsHttpEndpointBinding" name="wsHttpEndpoint" contract="PartnerComm.ContentXpert.Server.ICXPClient" />
<endpoint address="mex" binding="wsHttpBinding" bindingConfiguration="wsHttpEndpointBinding" name="mexEndpoint" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="https://localhost:8371/Design_Time_Addresses/Server/CXPClient/" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="requireCertificate">
<serviceMetadata httpsGetEnabled="true" />
<serviceCredentials>
<serviceCertificate findValue="CyberdyneIndustries" storeLocation="LocalMachine" storeName="TrustedPeople" x509FindType="FindBySubjectName"/>
<clientCertificate>
<authentication certificateValidationMode="ChainTrust" trustedStoreLocation="LocalMachine" />
</clientCertificate>
</serviceCredentials>
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<wsHttpBinding>
<binding name="wsHttpEndpointBinding" maxBufferPoolSize="5242880" maxReceivedMessageSize="5242880">
<readerQuotas maxDepth="32" maxStringContentLength="5242880" maxArrayLength="1073741824" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<security mode="Transport">
<transport clientCredentialType="Certificate" />
</security>
</binding>
</wsHttpBinding>
</bindings>
</system.serviceModel>

这是我的客户端配置:

  <system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="wsHttpEndpoint" closeTimeout="00:01:00" openTimeout="00:01:00"
receiveTimeout="00:10:00" sendTimeout="00:01:00" bypassProxyOnLocal="false"
transactionFlow="false" hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text"
textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<reliableSession ordered="true" inactivityTimeout="00:10:00"
enabled="false" />
<security mode="Transport">
<transport clientCredentialType="Certificate" />
</security>
</binding>
</wsHttpBinding>
</bindings>
<client>
<endpoint address="https://localhost:8371/Design_Time_Addresses/Server/CXPClient/"
binding="wsHttpBinding" bindingConfiguration="wsHttpEndpoint" behaviorConfiguration="ClientCertificateBehavior"
contract="ContentXPertServer.ICXPClient" name="wsHttpEndpoint" />
</client>
<behaviors>
<endpointBehaviors>
<behavior name="ClientCertificateBehavior">
<clientCredentials>
<clientCertificate x509FindType="FindBySubjectName" findValue="CyberdyneIndustries" storeLocation="LocalMachine" storeName="TrustedPeople" />
</clientCredentials>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>

当 security mode='None' 通过 http 时,所有代码都可以完美运行,但是当然,没有身份验证,System.ServiceModel.ServiceSecurityContext 中也没有任何内容。我已经对所有这些元素尝试了数十种变体,但都不可避免地以请求抛出异常“现有连接被远程主机强行关闭”而告终。

我使用的是自签名证书“​​Cyber​​dyneIndustries”,我已将其 CA 证书添加到受信任的 CA 存储区。当我查看证书时,证书会 check out 。我经历了 http 命名空间管理的 hell ,也解决了这些问题。看起来 WCF 并不真正支持这个...请告诉我我错了。

TIA。

最佳答案

最终,我决定尝试消息安全,看看它是否能揭示一些情况 - 它确实如此,我将减少损失并接受它。因此,对此没有明确的答案。

然而,实现消息安全确实暴露了一个大问题,这可能是传输安全问题的根源。 MSDN 上有一段毒文档:

http://msdn.microsoft.com/en-us/library/ff650751.aspx

在该页面,创建自签名证书的命令如下:

makecert -sk MyKeyName -iv RootCaClientTest.pvk -n "CN=tempClientcert" -ic RootCaClientTest.cer -sr currentuser -ss my -sky signature -pe

参数“signature”应该改为“exchange”。一旦我重新生成了所有证书,消息安全性就开始工作了。所有这一切的一大收获是,如果您想要实现传输安全,请首先让消息安全发挥作用,因为您从系统获得的错误消息更具描述性。

关于wcf - 通过 HTTPS 上的客户端证书对 WCF 请求进行身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4351034/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com