SSL连接到SQL Server时出现问题

Dis*_*ted 3 sql-server ssl

我想使用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();`
Run Code Online (Sandbox Code Playgroud)

注意 encrypt=true;

但是有connection.Open()一个异常是用消息引发的

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

我可以批准这个自签名证书吗?

Dav*_*cre 7

您需要告诉您的客户信任所提供的证书.自签名证书将不受信任,因为它未由您的计算机信任的任何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();
Run Code Online (Sandbox Code Playgroud)


Pre*_*gha 2

为了使 RPC over Http 正常工作,您必须安装并配置受信任的 CA 根证书。在使用自签名证书的情况下,您需要将证书安装到受信任的根证书颁发机构存储中。

将证书安装到您的受信任根存储中。