2012-01-01 15 views
6

Come si attacca un certificato quando si utilizza s SqlConnection? Da SqlConnection Connection String Parameter Keywords & Values, so che posso impostare Encrypted su true per forzare (incoraggiare?) L'uso di SSL/TLS..Net SqlConnection, Autenticazione server e Pinning certificato

Tuttavia, al pin un certificato, credo che abbiamo bisogno di usare ServerCertificateValidationCallback da ServicePointManager (codice di esempio riportato di seguito è stato offerto da Arne Vajhøj per HTTP/HTTPS). Non è chiaro come collegare PinCertificate (da ServicePointManager) a SqlConnection.

AGGIORNAMENTO: Parlando con Arne Vajhøj su microsoft.public.dotnet.languages.csharp, non è possibile avere il controllo desiderato sulla connessione. Vajhøj ha offerto un collegamento a 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

Un esempio di convalida dei certificati in VB.NET: http://social.msdn.microsoft.com/forums/en-US/netfxnetcom/thread/ 5f5af31c-74f2-4147-8b70-e9e8ec03c76a. Dovrebbe essere abbastanza facile da convertire. –

+0

L'esempio MSDN utilizza 'ServicePointManager' e' ServerCertificateValidationCallback' (chiamando 'MyCertValidationCb'). Non è diverso dal campione che ho postato. Non sono ancora chiaro su come un cavo 'ServerCertificateValidationCallback' in un' SqlConnection'. – jww

+0

http://support.microsoft.com/default.aspx?scid=276553 fa questo aiuto? – King

risposta

0

come su qualcosa di simile:

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 
Problemi correlati