c# - 为客户端服务器身份验证寻找 SSLStream.AuthenticateAsClient() 的替代方法
问题描述
在我的类库代码中,使用 SSLStream.AuthenticateAsClient() 进行 ssl 连接和握手,以及使用证书对服务器和客户端进行身份验证。但是上面的函数使用的targetHost
是服务器证书的CommonName
或SAN
值。如果传递空字符串,则调用 ValidateServerCertificate() 并将sslPolicyErrors
其设置为RemoteCertificateNameMismatch
.
因此,想要摆脱 的使用targethost
,是否有任何其他方法可以使此 SSL 连接以及客户端-服务器证书身份验证而不使用服务器 CN 或 SAN 值。
场景:我们有多个服务器,库可以在运行时连接到任何服务器。没有获取服务器CN
或SAN
调用AuthenticateAsClient
函数之前的方法。
因此,有任何其他 API 或方法可用于在不提供服务器证书CN
或SAN
.
public void Connection()
{
var sslStream = new SslStream(_client.GetStream(),true,
new RemoteCertificateValidationCallback(ValidateServerCertificate),
new LocalCertificateSelectionCallback(SelectLocalCertificate));
// targetHost is the server certificate CN or SAN.
if (IsClientCertProvided != null)
{
// get the client certificates locally and append to clientCertificates.
// client authentication.
sslStream.AuthenticateAsClient(targetHost, clientCertificates, sslProtocol,checkCertificateRevocation);
}
else
{
// connection with server authentication.
sslStream.AuthenticateAsClient(targetHost, null, sslProtocol, checkCertificateRevocation);
}
}
解决方案
推荐阅读
- gradle - Corda - gradle 依赖项:编译、cordaCompile 还是 cordapp?
- php - 我对访问 php 中的类以在 OOP 中回显数据感兴趣?
- graphql - 即使数据存在,`User` 类型的字段也会为 Query 返回 `null`
- c - 如何使用 malloc 或其他函数在 Ram 中分配所需的地址?
- jquery - 将 div 设置为右侧屏幕外的动画
- python - 如何在字符串中交替使用大写和小写字符?
- php - 如何解决 time() vs microtime() 问题?
- r - 如何将原始数据框中的列添加到抓取的数据中?
- python-3.x - ImportError:无法从“操作员”导入名称“itemgetter”
- javascript - 如何从对象返回值?