gpt4 book ai didi

wcf - 带有 RESTful WCF 服务的基于证书的相互 SSL

转载 作者:太空宇宙 更新时间:2023-11-03 15:03:05 24 4
gpt4 key购买 nike

我有一个基于证书的相互(双向)SSL WCF RESTful 服务,它托管在一个 Windows 应用程序中。服务器使用绑定(bind)了自签名证书的端口。该证书存在于“我的”商店的“LocalMachine”中。在同一个“我的”商店中还有客户证书。客户端和服务器证书的名称都与机器名称相同。客户端和服务器配置如下:

客户:

<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="ClientBehavior">
<webHttp />
<clientCredentials>
<clientCertificate storeLocation="LocalMachine" x509FindType="FindByThumbprint" storeName="My" findValue="F50C62754783EC741F6E84E25888D17CBC145691" />
<serviceCertificate>
</serviceCertificate>
</clientCredentials>
</behavior>
</endpointBehaviors>
</behaviors>
<bindings>
<webHttpBinding>
<binding name="WebHttpBinding_Conf">
<security mode="Transport">
<transport clientCredentialType="Certificate"/>
</security>
</binding>
</webHttpBinding>
</bindings>
<client>
<endpoint address="https://mymachine:8088/Service" behaviorConfiguration="ClientBehavior"
binding="webHttpBinding" bindingConfiguration="WebHttpBinding_Conf"
contract="RESTfulLib.IService" name="WebHttpBinding_NAme" />
</client>

服务器:

  <system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceCredentials>
<serviceCertificate storeLocation="LocalMachine" x509FindType="FindByThumbprint" findValue="7975794831242F2D39ED3B1BC8323EAF5DA2CA11" storeName="My"/>
</serviceCredentials>
<serviceDebug includeExceptionDetailInFaults="True"/>
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<webHttpBinding>
<binding name="WebHttpBinding_Conf">
<security mode="Transport">
<transport clientCredentialType="Certificate" />
</security>
</binding>
</webHttpBinding>
</bindings>
<services>
<service name="RESTfulLib.Service" behaviorConfiguration="ServiceBehavior">
<host>
<baseAddresses>
<add baseAddress="https://mymachine:8088/Service"/>
</baseAddresses>
</host>
<endpoint address="" behaviorConfiguration="web" binding="webHttpBinding" bindingConfiguration="WebHttpBinding_Conf" contract="RESTfulLib.IService">
</endpoint>
</service>
</services>

但是,我在 SSL 握手期间收到此错误:

The HTTP request was forbidden with client authentication scheme 'Anonymous'.

启用详细的 WCF 日志显示我们在握手期间出现此错误:

System.Net Error: 0 : [11504] Decrypt returned SEC_I_RENEGOTIATE.

我试过这个设置,但这也没有帮助:

            ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;

证书也被复制到受信任的根 CA 存储,并且有一个 ServicePointManager.ServerCertificateValidationCallback 在所有情况下都返回 true(!)

任何答案将不胜感激。谢谢!

最佳答案

由于您的证书是自签名的,因此您必须为您的客户提供一些额外的代码。

在实例化您的 WCF 客户端之前,您应该添加:

ServicePointManager.ServerCertificateValidationCallback = TrustAllCertificatesCallback;

TrustAllCertificatesCallback 是执行服务证书验证的回调方法。这是一个示例:

internal static bool TrustAllCertificatesCallback(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors errors)
{
X509Certificate2 certificate = new X509Certificate2(cert);
return certificate.Verify();
}

您还必须在受信任的根证书颁发机构证书存储中安装您的证书,最后添加到您的客户端端点配置:

<client>
<endpoint address="https://mymachine:8088/Service" behaviorConfiguration="ClientBehavior"
binding="webHttpBinding" bindingConfiguration="WebHttpBinding_Conf"
contract="RESTfulLib.IService" name="WebHttpBinding_NAme">
<identity>
<dns value="<<<your certificate name>>>" />
</identity>
</endpoint>
</client>

您必须确保您的证书已正确安装并配置为可访问:

  1. 您首先必须在本地计算机 Peronnal 存储中导入证书的私钥,而不是在当前用户的Peronnal 存储中。如果服务帐户必须使用证书,这一点很重要。

  2. 如果您使用自签名证书,请确保公共(public)证书安装在本地计算机受信任的根权限存储中。

  3. 允许用户访问您的证书:右键单击私有(private)证书(本地计算机/个人),然后依次选择“所有任务”和“管理私钥”。

关于wcf - 带有 RESTful WCF 服务的基于证书的相互 SSL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18013587/

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