SqlException: 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)
ADO.NETでSQLServerに接続を試みると、Errorとなった。
AuthenticationException: The remote certificate was rejected by the provided RemoteCertificateValidationCallback.
"リモート証明書は、提供された RemoteCertificateValidationCallback によって拒否されました。"
と表示された。
実行環境
- .NET6
- Microsoft.Data.SqlClient 5.0.0
RemoteCertificateValidationCallback
は、リモートSSL (Secure Sockets Layer) 証明書を検証してbooleanを返すメソッドだった。
DB接続文字列を修正
connectionString
に渡す接続文字列にTrustServerCertificate=True
を追加した。
"TrustServerCertificate を true に設定した場合、トランスポート層に SSL が使用されてチャネルが暗号化されます"とのこと。
"Server={server};Database={database};User Id={id};Password={password};" +
"TrustServerCertificate=True;"
Reference