一个SSL报错的问题 返回
数据库是装在Windows服务器上的,程序是在麒麟10系统上,启动就报错
中文提示 : 连接数据库过程中发生错误,检查服务器是否正常连接字符串是否正确,错误信息: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)DbType="SqlServer";ConfigId="".
English Message : Connection open error . 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)DbType="SqlServer";ConfigId=""
.NET版本是.NET 5,sqlsugar版本5.1.4.151,SQLserver版本是2012 SP1
连接字符串
Data Source=*****;Initial Catalog=******;User ID=******;Password=********;MultipleActiveResultSets=true;Encrypt=False;TrustServerCertificate=True
我试过很多连接字符串写法都不行,怎么样最小成本的解决这个问题,我现在客户单位处理这个问题,搞不定啊!
热忱回答(14)
-
摇曳的风筝 VIP0
1个月前using SqlSugar; using System; using System.Collections.Generic; using System.Net; using System.Net.Security; using System.Security.Cryptography.X509Certificates; using System.Web; public class Program { public static void Main(string[] args) { Console.WriteLine("程序启动..."); // ⭐⭐⭐ 关键:在创建任何连接之前配置安全设置 ⭐⭐⭐ ConfigureGlobalSecurity(); // 测试连接 TestSqlServerConnections(); // 如果是 Web 应用,继续启动 // CreateHostBuilder(args).Build().Run(); } private static void ConfigureGlobalSecurity() { Console.WriteLine("配置全局 SSL/TLS 安全设置..."); // 1. 接受所有 SSL/TLS 证书(包括自签名和弱签名) ServicePointManager.ServerCertificateValidationCallback = (object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) => { // 记录但不阻止 if (sslPolicyErrors != SslPolicyErrors.None) { Console.WriteLine($"SSL 证书警告(已忽略): {sslPolicyErrors}"); Console.WriteLine($" 证书主题: {certificate.Subject}"); Console.WriteLine($" 证书颁发者: {certificate.Issuer}"); } return true; // ⭐ 总是接受证书 ⭐ }; // 2. 设置支持的 TLS 版本(SQL Server 2012 需要 TLS 1.0) ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | // TLS 1.0 SecurityProtocolType.Tls11 | // TLS 1.1 SecurityProtocolType.Tls12; // TLS 1.2 // 3. 禁用证书吊销检查 ServicePointManager.CheckCertificateRevocationList = false; // 4. 其他性能优化 ServicePointManager.Expect100Continue = false; ServicePointManager.UseNagleAlgorithm = false; ServicePointManager.DefaultConnectionLimit = 100; Console.WriteLine("✅ 全局安全配置完成"); } private static void TestSqlServerConnections() { List<string> conns = new(); // 方法1:URL 编码密码(可能不需要) string password = HttpUtility.UrlEncode("******"); string connectionString = $"Server=******;Database=IOT_PlatForm;User Id=******;Password={password};MultipleActiveResultSets=true;Encrypt=False;TrustServerCertificate=True;"; conns.Add(connectionString); // 方法2:标准连接字符串 conns.Add("Data Source=******;Initial Catalog=IOT_PlatForm;User ID=******;Password=******;MultipleActiveResultSets=true;Encrypt=False;TrustServerCertificate=True"); // 方法3:简单连接字符串 conns.Add("Server=******;Database=IOT_PlatForm;User Id=******;Password=******;MultipleActiveResultSets=true;Encrypt=False;TrustServerCertificate=True;"); // 方法4:添加更多选项的连接字符串 conns.Add("Server=******;Database=IOT_PlatForm;User Id=******;Password=******;Encrypt=False;TrustServerCertificate=True;Connection Timeout=30;Pooling=true;"); Console.WriteLine($"\n开始测试 {conns.Count} 种连接方式..."); for (int i = 0; i < conns.Count; i++) { Console.WriteLine($"\n=== 测试方式 {i + 1} ==="); // 隐藏密码显示 string displayConn = conns[i].Replace("******", "***") .Replace(password, "***"); Console.WriteLine($"连接字符串: {displayConn}"); try { using (SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() { ConnectionString = conns[i], DbType = DbType.SqlServer, IsAutoCloseConnection = true })) { db.Open(); Console.WriteLine("✅ 数据库连接成功!"); // 测试查询 var version = db.Ado.GetString("SELECT @@VERSION"); Console.WriteLine($"SQL Server 版本: {version}"); var dbName = db.Ado.GetString("SELECT DB_NAME()"); Console.WriteLine($"当前数据库: {dbName}"); return; // 成功就退出 } } catch (Exception ex) { Console.WriteLine($"❌ 连接失败: {ex.Message}"); if (ex.InnerException != null) { Console.WriteLine($" 内部错误: {ex.InnerException.Message}"); } } } Console.WriteLine("\n⚠️ 所有连接方式都失败了!"); } }0 回复 -
摇曳的风筝 VIP0
1个月前全部都失败了,我从SQLserver服务器上导出了证书在linux上安装了
0 回复 -
fate sta VIP0
1个月前11.3 successfully established
Microsoft.Data.SqlClient.SqlException (0x80131904): A connection was successfully established with the server, but then an error occurred during the login process. (provider: SSL Provider, error: 0 - The certificate chain was issued by an authority that is not trusted.) ---> System.ComponentModel.Win32Exception (0x80090325): The certificate chain was issued by an authority that is not trusted.
方案1. 在客户端环境中安装目标 SQL Server 的 TLS/SSL 证书。 如果需要加密,将对其进行验证。
方案2.(不太安全)在连接字符串中设置“TrustServerCertificate=true”属性。
方案3.(不安全的解决方案)在 docker 映像/客户端环境中配置 TLS/SSL 设置,以便与 TLS 1.0 连接。
MinProtocol = TLSv1
CipherString = DEFAULT@SECLEVEL=1
0 回复 -
fate sta VIP0
1个月前0 回复 -
摇曳的风筝 VIP0
1个月前方案1和方案2 都试过了不行,方案3是要改程序吗
我现在是完全搞不懂了,我看SQLserver的设置里面是没有强制加密的,也没有关联证书
为什么死活连不上啊
要奔溃了
AI说这是因为linux系统的安全要求比较高的缘故?
0 回复 -
摇曳的风筝 VIP0
1个月前方案3具体是怎么操作呢
0 回复 -
摇曳的风筝 VIP0
1个月前改连接字符串的那个我试了好几种,你看我发的代码里,AI生成的,都是连不上,简直要疯了啊这个问题
0 回复 -
fate sta VIP0
1个月前new SqlConnection(字符串).Open() 原生能用ORM就能用。上面有微软链接 。就几种情况。
0 回复 -
摇曳的风筝 VIP0
1个月前sqlsugar用的
Microsoft.Data.SqlClient
还是
System.Data.SqlClient
我把sqlserver的证书导入到ubuntu系统了,AI给的结论

这个错误表明 SSL/TLS 握手失败。问题可能是 SqlSugar 没有使用 .NET 的系统级证书验证设置
我再写个demo用原生的ADO对象试试看
0 回复 -
fate sta VIP0
1个月前Microsoft.Data.SqlClient
0 回复 -
摇曳的风筝 VIP0
1个月前System.Data.SqlClient is not supported on this platform.
使用Microsoft.Data.SqlClient又报这个错
try { using (var connection = new Microsoft.Data.SqlClient.SqlConnection(connString)) { connection.Open(); using (var command = new Microsoft.Data.SqlClient.SqlCommand("SELECT @@VERSION, DB_NAME()", connection)) using (var reader = command.ExecuteReader()) { if (reader.Read()) { return $"✅ 直接连接成功!\n版本: {reader.GetString(0)}\n数据库: {reader.GetString(1)}"; } } } return "✅ 连接成功"; } catch (Exception ex) { return $"❌ 直接连接失败: {ex.Message}"; }Object reference not set to an instance of an object.
连接字符串都加了Encrypt=true;TrustServerCertificate=true;
0 回复 -
摇曳的风筝 VIP0
1个月前
用这个sqlcmd工具是通的

sqlsugar似乎是不支持FreeTDS啊
0 回复 -
fate sta VIP0
1个月前和sqlsugar关没有毛线关系,
Microsoft.Data.SqlClient
这个能用,就可以用。
0 回复 -
摇曳的风筝 VIP0
1个月前@fate sta:
问题解决了,安装sp4补丁就行了。
经过测试,最后三种写法不行,前面四种是可以的。
0 回复