在 .net core 3.1 的 docker 映象生成的容器中,連線 sql server 2008 r2 版本的資料庫,遇到錯誤

不是豆豆發表於2024-04-30

錯誤1:

{
    "ClassName": "System.Data.SqlClient.SqlException",
    "Message": "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)",
    "Data": {
        "HelpLink.ProdName": "Microsoft SQL Server",
        "HelpLink.EvtSrc": "MSSQLServer",
        "HelpLink.EvtID": "0",
        "HelpLink.BaseHelpUrl": "https://go.microsoft.com/fwlink",
        "HelpLink.LinkId": "20476",
        "SqlError 1": "System.Data.SqlClient.SqlError: 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)"
    },
    "InnerException": {
        "ClassName": "System.Security.Authentication.AuthenticationException",
        "Message": "Authentication failed, see inner exception.",
        "Data": null,
        "InnerException": {
            "StackTrace": "   at Interop.OpenSsl.DoSslHandshake(SafeSslHandle context, Byte[] recvBuf, Int32 recvOffset, Int32 recvCount, Byte[]& sendBuf, Int32& sendCount)\n   at System.Net.Security.SslStreamPal.HandshakeInternal(SafeFreeCredentials credential, SafeDeleteContext& context, ArraySegment`1 inputBuffer, Byte[]& outputBuffer, SslAuthenticationOptions sslAuthenticationOptions)",
            "Message": "SSL Handshake failed with OpenSSL error - SSL_ERROR_SSL.",
            "Data": {},
            "InnerException": {
                "StackTrace": null,
                "Message": "error:1425F102:SSL routines:ssl_choose_client_version:unsupported protocol",
                "Data": {},
                "InnerException": null,
                "HelpLink": null,
                "Source": null,
                "HResult": 338030850
            },
            "HelpLink": null,
            "Source": "System.Net.Security",
            "HResult": -2146233088
        },

問題和解決方法:

應該是 SQL Server 2008 R2 或其所在伺服器支援的加密協議版本過低(不再安全),新版客戶端已不再支援。

修改容器中 openssl 檔案配置的最低允許使用的協議版本即可:

sed -i -e "s|^MinProtocol = .*|MinProtocol = TLSv1.0|g" "/etc/ssl/openssl.cnf"

另附,查詢當前系統支援的加密協議:

openssl ciphers -v | awk '{print $2}' | sort | uniq

參考連結:https://stackoverflow.com/questions/71527736/c-sharp-sslexception-ssl-handshake-failed-with-openssl-error-ssl-error-ssl

但解決了上一個問題,接下來就遇到了

錯誤2:

{
    "ClassName": "System.Data.SqlClient.SqlException",
    "Message": "Connection Timeout Expired.  The timeout period elapsed during the post-login phase.  The connection could have timed out while waiting for server to complete the login process and respond; Or it could have timed out while attempting to create multiple active connections.  The duration spent while attempting to connect to this server was - [Pre-Login] initialization=0; handshake=9; [Login] initialization=0; authentication=0; [Post-Login] complete=14454; ",
    "Data": {
        "HelpLink.ProdName": "Microsoft SQL Server",
        "HelpLink.EvtSrc": "MSSQLServer",
        "HelpLink.EvtID": "-2",
        "HelpLink.BaseHelpUrl": "https://go.microsoft.com/fwlink",
        "HelpLink.LinkId": "20476",
        "SqlError 1": "System.Data.SqlClient.SqlError: Connection Timeout Expired.  The timeout period elapsed during the post-login phase.  The connection could have timed out while waiting for server to complete the login process and respond; Or it could have timed out while attempting to create multiple active connections.  The duration spent while attempting to connect to this server was - [Pre-Login] initialization=0; handshake=9; [Login] initialization=0; authentication=0; [Post-Login] complete=14454; "
    },
    "InnerException": {
        "ClassName": "System.ComponentModel.Win32Exception",
        "Message": "Unknown error 258",
        "Data": null,
        "InnerException": null,
        "HelpURL": null,
        "StackTraceString": null,
        "RemoteStackTraceString": null,
        "RemoteStackIndex": 0,
        "ExceptionMethod": null,
        "HResult": -2147467259,
        "Source": null,
        "WatsonBuckets": null,
        "NativeErrorCode": 258
    },

問題和解決方法:

暫未找到,同樣的程式在 windows 下暫未遇到此問題。

相關文章