Tag: dot net core

  • [Solved] .Net Core 在 Linux(Ubuntu) 連線 SQL 發生 SSL Handshake Error

    # 問題描述 Issue #
    .Net Core 5 程式使用 SqlClient 在 Linux (Ubuntu 20.04) 上連線較舊版本的 SQL Server 時,出現以下類似的錯誤訊息:
    Error Microsoft.Data.SqlClient.SqlException: A connection was successfully established with the server, but then an error occurred during the pre-login handshake. (provider: SSL Provider, error: 31 - Encryption(ssl/tls) handshake failed)

    # 問題原因 Reason #
    1) 較舊版本的 SQL Server 不支援 TLS 1.2 或更新版本的傳輸加密,而 Linux 所使用的 OpenSSL 預設為 TLS 1.2 或更新版本。並進而 導致連線 Sql Client 無法與 SQL Server 進行加密傳輸。
    2) .Net Core 5 程式使用的 Sql Client 經測試,在 Linux 上無法透過 Connection String 的 Encrypt=false 參數來強制關閉加密。

    # 解決方法 Solution #
    1) 編輯 OpenSSL 設定:sudo vi /etc/ssl/openssl.cnf
    2) 在最上面(讓其他設定以此為基礎)加入:openssl_conf = default_conf
    3) 接著加入以下設定:
    [default_conf]
    ssl_conf = ssl_sect

    [ssl_sect]
    system_default = system_default_sect

    [system_default_sect]
    MinProtocol = TLSv1
    CipherString = DEFAULT:@SECLEVEL=1

    4) 在 Connection String 中加入 TrustServerCertificate=true 以自動信任 SQL Server 的憑證。
    5) 重啟 OpenSSL 服務