2011-08-11 60 views
2

我想使用SSL連接到本地SQL Server 2008數據庫。SSL連接到SQL Server時出現問題

我沒有,因爲根據該http://msdn.microsoft.com/en-us/library/ms189067.aspx

If a trusted certificate is not installed, SQL Server will generate a self-signed certificate when the instance is started, and use the self-signed certificate to encrypt the credentials.

在服務器上安裝任何證書,這是簡單的代碼

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()的例外是用訊息舉報

A connection was successfully established with the server, but then an error occurred during the pre-login handshake. (provider: SSL Provider, error: 0 - The certificate chain was issued by an authority that is not trusted.)

我可以批准這個自簽名證書嗎?

回答

2

In order for RPC over Http to work you must have a Trusted CA Root Certificate installed and configured. In a situation where you are using a self-signed cert you will need to install the certificate into the Trusted Root Certification Authorities store.

Install the cert into your trusted root store.

+0

實際上並非如此。 SQL Server將在實例啓動時生成一個自簽名證書 – Disappointed

6

你需要告訴你的客戶信任,提出任何證書。自簽名證書不會被信任,因爲它沒有被您的計算機信任的任何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();