簡體   English   中英

從 Linux 主機連接到 SQL 服務器時,遠程證書被拒絕

[英]Remote certificate rejected when connecting to SQL Server from Linux host

我已獲得 SQL 服務器的證書,已成功安裝並激活。 我可以從日志中確認這一點:

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 上的證書的處理是錯誤的。

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

將證書導入 SQL Configuration Manager 后,您可以從兩個相同的條目中進行選擇。 區別在於一個具有友好名稱host.domain (FQDN),而另一個沒有友好名稱。 當然,我沒有仔細檢查,只是保留了沒有 FQDN 作為友好名稱的那個,它是在導入后預先選擇的,看起來有效。 第二個錯誤是沒有意識到 WSL 不是我們域的一部分,無法解析數據庫名稱。 所以我習慣於使用 FQDN 來訪問它。 這導致了數據庫實例預期的host與 WSL 內部客戶端使用的host.domain之間的不匹配。

在此之后,我在我們的域中借用了一個 Ubuntu 虛擬機並驗證了,在連接字符串中僅使用host,port模式是有效的。

為了使其明確在 wsl 內部工作,我切換了證書以在連接時要求 FQDN 作為友好的 namae,因此可以從域內部以及“外部”(從 wsl)建立連接。

tl;dr :對異常The remote certificate was rejected by the provided RemoteCertificateValidationCallback. 也就是說,連接字符串中的實例名稱與實例中的預期名稱不匹配。

如果您正在連接一些不重要的東西,例如我在從我的 do.net 核心 API 連接到包含在 docker 容器中的 Microsoft sql 服務器時遇到此錯誤,所有這些都是在本地進行開發工作。 我的解決方案是將"DefaultConnection": "server=localhost;database=newcomparer;trusted_connection=false;User Id=sa;Password=reallyStrongPwd123;Persist Security Info=False;Encrypt=False" =False 放在我的連接字符串的末尾,如下所示: "DefaultConnection": "server=localhost;database=newcomparer;trusted_connection=false;User Id=sa;Password=reallyStrongPwd123;Persist Security Info=False;Encrypt=False"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM