连接SQL Server时SSL连接出现问题

5
我想使用SSL连接到我的本地SQL Server 2008数据库。
我没有在服务器上安装任何证书,因为根据这个http://msdn.microsoft.com/en-us/library/ms189067.aspx

如果没有安装受信任的证书,SQL Server会在实例启动时生成自签名证书,并使用自签名证书加密凭据。

这是简单的代码。
SqlConnection connection = 
    new SqlConnection(@"Data Source=somePC\SqlExpress;Initial Catalog=test;integrated security = sspi;Persist Security Info=True;Encrypt=true;");

SqlCommand command = new SqlCommand("Select count(*) from Employee", connection);
connection.Open();
int employeeCount = (int)command.ExecuteScalar();
connection.Close();`

请注意,encrypt=true; 但是在connection.Open()时会引发异常,异常信息如下:

已经成功与服务器建立连接,但在预登录握手期间发生错误。(提供程序: SSL提供程序,错误: 0 - 证书链由不受信任的授权机构颁发。)

我该如何批准这个自签名证书?
2个回答

10

您需要告诉客户端信任呈现的任何证书。自签名证书将不受信任,因为它没有被计算机信任的任何CA签名。通过添加TrustServerCertificate=True更改连接,如下所示:

SqlConnection connection = 
    new SqlConnection(@"Data Source=somePC\SqlExpress;Initial Catalog=test;integrated security = sspi;Persist Security Info=True;Encrypt=true; TrustServerCertificate=True");

SqlCommand command = new SqlCommand("Select count(*) from Employee", connection);
connection.Open();
int employeeCount = (int)command.ExecuteScalar();
connection.Close();

2

实际上我没有它。SQL Server在启动实例时会生成自签名证书。 - Disappointed

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接