2012-01-01 105 views
6

如何在使用s SqlConnection時鎖定證書?從SqlConnection Connection String Parameter Keywords & Values,我知道我可以設置Encryptedtrue強制(鼓勵?)使用SSL/TLS。.Net SqlConnection,服務器身份驗證和證書鎖定

但是,爲了獲得證書,我相信我們需要使用 ServicePointManager(以下示例代碼由ArneVajhøj提供,用於HTTP/HTTPS)。我不清楚如何將PinCertificate(從ServicePointManager)連線到SqlConnection

更新:與microsoft.public.dotnet.languages.csharp上的ArneVajhøj交談,似乎無法對連接進行所需的控制。 Vajhøj提供了鏈接Encrypting Connections to SQL Server

public static void Main(string[] args) 
{ 
    ServicePointManager.ServerCertificateValidationCallback = PinCertificate; 
    WebRequest wr = WebRequest.Create("https://www.google.com/"); 

    wr.GetResponse(); 
} 

public static bool PinCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) 
{ 
    byte[] chash = certificate.GetCertHash(); 

    StringBuilder sb = new StringBuilder(chash.Length * 2); 
    foreach (byte b in chash) 
    sb.AppendFormat("{0:X2}", b); 

    // Verify against known SHA1 thumb print of the certificate 
    String hash = sb.ToString(); 
    if (hash != "C1956DC8A7DFB2A5A56934DA09778E3A11023358") 
    return false; 

    return true; 
} 
+0

在VB.NET證書驗證的例子:http://social.msdn.microsoft.com/forums/en-US/netfxnetcom/thread/ 5f5af31c-74f2-4147-8b70-e9e8ec03c76a。應該很容易轉換。 – 2012-01-01 06:21:59

+0

MSDN示例使用「ServicePointManager」和「ServerCertificateValidationCallback」(調用「MyCertValidationCb」)。它與我發佈的樣本沒有什麼不同。我仍然不清楚如何將'ServerCertificateValidationCallback'連接到'SqlConnection'。 – jww 2012-01-01 06:53:00

+0

http://support.microsoft.com/default.aspx?scid=276553對此有幫助嗎? – King 2012-01-06 23:00:42

回答

0

怎麼是這樣的:

System.Net.ServicePointManager.ServerCertificateValidationCallback = New RemoteCertificateValidationCallback(AddressOf ValidateCertificate) 

Private Function ValidateCertificate(ByVal sender As Object, ByVal certificate As X509Certificate, ByVal chain As X509Chain, ByVal sslPolicyErrors As SslPolicyErrors) As Boolean 
    'Return True to force the certificate to be accepted. 
    Return True 
End Function