【问题标题】:Remote certificate rejected when connecting to SQL Server from Linux host从 Linux 主机连接到 SQL Server 时远程证书被拒绝
【发布时间】:2023-01-14 10:39:22
【问题描述】:

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

The certificate [Cert Hash(sha1) "xxxxxxxxxxxxxxxxxxE5C050F7D75F58E4E2F"] was successfully loaded for encryption.

使用 SSMS 连接到数据库是成功的,只需在不信任服务器证书的情况下对连接进行加密即可。

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

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

var con = new SqlConnection("Server=domain.host.eu,50730;Database=databasename;User Id=user;Password='password';");
await con.OpenAsync();
var version = con.ExecuteScalar<string>("SELECT @@VERSION");
Console.WriteLine(version);

这有效,如果我将 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)

我想从 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

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

【问题讨论】:

  • 你看过这些问题了吗:#1535#1402
  • @AaronBertrand 我有,但不幸的是他们没有提供可行/有效的解决方案,除了信任服务器证书
  • Cleptus,我知道它正在工作,通过使用 SSMS 从 Windows 机器上测试它并从日志中验证,证书已加载。
  • 那么,您采取了哪些步骤将其添加到 Linux 上的 SQL Server?

标签: c# sql-server ubuntu ssl ssl-certificate


【解决方案1】:

我双目失明,同时被 2 次事故分散了注意力。

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

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

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

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

【讨论】:

    【解决方案2】:

    如果您正在连接一些不重要的东西,例如我在从我的 dotnet 核心 API 连接到包含在 docker 容器中的 Microsoft sql 服务器时遇到此错误,所有这些都是在本地进行开发工作。我的解决方案是将 encrypt=False 放在连接字符串的末尾,如下所示:"DefaultConnection": "server=localhost;database=newcomparer;trusted_connection=false;User Id=sa;Password=reallyStrongPwd123;Persist Security Info=False;Encrypt=False"

    【讨论】:

      猜你喜欢
      • 2016-03-27
      • 1970-01-01
      • 2020-07-11
      • 2022-11-24
      • 1970-01-01
      • 2013-09-20
      • 1970-01-01
      • 2020-09-21
      • 1970-01-01
      相关资源
      最近更新 更多