首页 > 解决方案 > 为客户端服务器身份验证寻找 SSLStream.AuthenticateAsClient() 的替代方法

问题描述

在我的类库代码中,使用 SSLStream.AuthenticateAsClient() 进行 ssl 连接和握手,以及使用证书对服务器和客户端进行身份验证。但是上面的函数使用的targetHost是服务器证书的CommonNameSAN值。如果传递空字符串,则调用 ValidateServerCertificate() 并将sslPolicyErrors其设置为RemoteCertificateNameMismatch.

因此,想要摆脱 的使用targethost,是否有任何其他方法可以使此 SSL 连接以及客户端-服务器证书身份验证而不使用服务器 CN 或 SAN 值。

场景:我们有多个服务器,库可以在运行时连接到任何服务器。没有获取服务器CNSAN调用AuthenticateAsClient函数之前的方法。

因此,有任何其他 API 或方法可用于在不提供服务器证书CNSAN.

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);
   }
}

标签: c#sslssl-certificatetls1.2.net-standard

解决方案


推荐阅读