从 Linux 主机连接到 SQL Server 时远程证书被拒绝

Mar*_*rco 13 c# sql-server ubuntu ssl ssl-certificate

我已获得 SQL Server 的证书,该证书已成功安装并激活。我可以从日志中确认这一点:

The certificate [Cert Hash(sha1) "xxxxxxxxxxxxxxxxxxE5C050F7D75F58E4E2F"] was successfully loaded for encryption.
Run Code Online (Sandbox Code Playgroud)

只需加密连接而不信任服务器证书,即可成功使用 SSMS 连接到数据库。

我想使用 WSL 和后来的 docker 来复制它。

我正在使用一个简单的 .net 6 控制台应用程序对此进行测试:

The certificate [Cert Hash(sha1) "xxxxxxxxxxxxxxxxxxE5C050F7D75F58E4E2F"] was successfully loaded for encryption.
Run Code Online (Sandbox Code Playgroud)

如果我添加Trust Server Certificate=True;到连接字符串,这将起作用。如果没有它,连接将失败:

Unhandled exception. Microsoft.Data.SqlClient.SqlException (0x80131904): A connection was successfully established with the server, but then an error occurred during the pre-login handshake. (provider: TCP Provider, error: 35 - An internal exception was caught)
 ---> System.Security.Authentication.AuthenticationException: The remote certificate was rejected by the provided RemoteCertificateValidationCallback.
   at System.Net.Security.SslStream.SendAuthResetSignal(ProtocolToken message, ExceptionDispatchInfo exception)
   at System.Net.Security.SslStream.CompleteHandshake(SslAuthenticationOptions sslAuthenticationOptions)
   at System.Net.Security.SslStream.ForceAuthenticationAsync[TIOAdapter](TIOAdapter adapter, Boolean receiveFirst, Byte[] reAuthenticationData, Boolean isApm)
   at System.Net.Security.SslStream.AuthenticateAsClient(SslClientAuthenticationOptions sslClientAuthenticationOptions)
   at System.Net.Security.SslStream.AuthenticateAsClient(String targetHost, X509CertificateCollection clientCertificates, SslProtocols enabledSslProtocols, Boolean checkCertificateRevocation)
Run Code Online (Sandbox Code Playgroud)

我想从 pfx 中提取证书:

openssl pkcs12 -in host.domain.eu.pem.pfx -clcerts -nokeys -out host.domain.eu.crt
sudo cp host.domain.eu.crt /usr/local/share/ca-certificates/
sudo update-ca-certificates
Run Code Online (Sandbox Code Playgroud)

不幸的是,这失败了,并出现相同的错误消息,我不知道哪里出了问题。我只能假设我在 Linux 上处理证书是错误的。

Eri*_*nez 34

如果您连接到不重要的东西,我在从 dotnetcore API 连接到 docker 容器(全部在本地)中包含的 microsoft sql 服务器以进行开发工作时遇到此错误。我的解决方案是encrypt=False像这样放在连接字符串的末尾(在 appsettings.json 中):

"DefaultConnection": "server=localhost;database=newcomparer;trusted_connection=false;User Id=sa;Password=reallyStrongPwd123;Persist Security Info=False;Encrypt=False"

  • 我并不完全不同意你的观点,但是我在谷歌上寻找解决方案将我带到了这里,我找到了解决我的简单开发环境问题的解决方案,并决定与其他人分享该解决方案...... (6认同)

Mar*_*rco 7

我让自己措手不及,同时因两起事故而分心。

将证书导入 SQL 配置管理器后,您可以从大约 2 个相同条目中​​进行选择。区别在于一个具有友好名称host.domain(FQDN),而另一个则没有友好名称。当然,我没有仔细检查,只是保留了没有 FQDN 作为友好名称的名称,该名称是在导入后预先选择的并且看起来有效。第二个错误是没有意识到 WSL 不是我们域的一部分,无法解析数据库名称。所以我使用 FQDN 来访问它。host这导致数据库实例期望的 和host.domainWSL 内部客户端使用的之间不匹配。

之后,我在我们的域内借用了一个 Ubuntu 虚拟机并进行了验证,仅使用host,port连接字符串中的模式是有效的。

为了使其明确在 wsl 内工作,我切换了证书,要求在连接时使用 FQDN 作为友好的名字,因此可以从域内部以及“外部”(来自 wsl)建立连接。

tl;dr:对该异常的一种可能的解释The remote certificate was rejected by the provided RemoteCertificateValidationCallback.是,连接字符串中的实例名称与实例中的预期名称不匹配。