我正在尝试使用 HttpClient
和以下代码发送自签名客户端证书:
var handler = new HttpClientHandler();
handler.ClientCertificateOptions = ClientCertificateOption.Manual;
handler.ClientCertificates.Add(GetClientCertificate()); //Loads cert from a .pfx file
var client = new HttpClient(handler);
PerformRequest(client);
我已经看过十几篇与此相关的 SO 帖子,但没有一个能解决我的问题。
注意事项:
HttpClient
并未发送。handler.SslProtocols
将 TLS 版本强制为 1.0、1.1、1.2 -- 无济于事X509Certificate2
返回的 GetClientCertificate()
有一个私钥我已经对此进行了 3 天的故障排除,甚至通读了参考资料,试图找出我的证书未包含在内的原因,但我找不到原因。
我怀疑在 HttpClient
实现的深处,我的证书被拒绝了,因为它是自签名的。
如何强制它发送我的证书? (服务器维护一个白名单,所以我不在乎它是否认为我的证书无效)。 或者,至少,我怎样才能从中获得任何类型的调试信息?有什么办法可以找到证书被拒绝的原因吗?
更新:
启用跟踪后,我发现 .NET 正确选择了我的证书:
System.Net Information: 0 : [23960] SecureChannel#64538993 - Selected certificate: <omitted>
System.Net Information: 0 : [23960] SecureChannel#64538993 - Left with 1 client certificates to choose from.
System.Net Information: 0 : [23960] SecureChannel#64538993 - Trying to find a matching certificate in the certificate store.
System.Net Information: 0 : [23960] SecureChannel#64538993 - Locating the private key for the certificate: <omitted>
System.Net Information: 0 : [23960] SecureChannel#64538993 - Certificate is of type X509Certificate2 and contains the private key.
System.Net Information: 0 : [23960] SecureChannel#64538993::.AcquireClientCredentials, new SecureCredential() (flags=(ValidateManual, NoDefaultCred, SendAuxRecord), m_ProtocolFlags=(Tls12Client), m_EncryptionPolicy=RequireEncryption)
System.Net Information: 0 : [23960] AcquireCredentialsHandle(package = Microsoft Unified Security Protocol Provider, intent = Outbound, scc = System.Net.SecureCredential)
System.Net Information: 0 : [23960] InitializeSecurityContext(credential = System.Net.SafeFreeCredential_SECURITY, context = 21c5cd98:21cd2108, targetName = <omitted>, inFlags = ReplayDetect, SequenceDetect, Confidentiality, AllocateMemory, InitManualCredValidation)
System.Net Information: 0 : [23960] InitializeSecurityContext(In-Buffers count=2, Out-Buffer length=100, returned code=ContinueNeeded).
... etc
然而,它仍然没有被发送。如果您有更多关于去哪里寻找的想法,我们将不胜感激。
答案 0 :(得分:0)
This answer 引导我找到解决方案。
--remove .0+ if no 1-9 after dot:
select regexp_replace(cast(figure as varchar),'\.0+$','')
from mydata
抛出一个 X509Certificate2.PrivateKey
因为我使用 ECD 作为签名算法。我切换到 RSA,现在它可以正确发送证书。
奇怪的是,跟踪显示 ECD 证书没有问题,并且能够成功识别私钥。我不知道为什么会这样——这对我来说听起来像是一个错误。尽管如此,RSA 将适用于我的用例,我已经厌倦了调试它。