【发布时间】:2012-01-31 07:02:58
【问题描述】:
在使用SqlConnection 时如何固定证书?从SqlConnection Connection String Parameter Keywords & Values,我知道我可以将Encrypted 设置为true 以强制(鼓励?)使用SSL/TLS。
但是,要固定证书,我认为我们需要使用来自ServicePointManager 的ServerCertificateValidationCallback(下面的示例代码由 Arne Vajhøj 提供用于 HTTP/HTTPS)。我不清楚如何将PinCertificate(来自ServicePointManager)连接到SqlConnection。
更新:在 microsoft.public.dotnet.languages.csharp 上与 Arne Vajhø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;
}
【问题讨论】:
-
VB.NET 中的证书验证示例:social.msdn.microsoft.com/forums/en-US/netfxnetcom/thread/…。应该很容易转换。
-
MSDN 示例使用
ServicePointManager和ServerCertificateValidationCallback(调用MyCertValidationCb)。它与我发布的示例没有什么不同。我仍然不清楚如何将ServerCertificateValidationCallback连接到SqlConnection。 -
我不太清楚这一点,但我认为当您建立使用加密连接(SSL)的连接时,它实际上会访问本地证书哈希存储以获取预期证书,当服务点管理器可以解析有链接。
-
"TrustServerCertificate 关键字是 ADO.NET 2.0 中的新关键字,仅在使用有效证书连接到 SQL Server 2005 实例时才有效。当 TrustServerCertificate 设置为 true 时,传输层将使用 SSL 进行加密通道并绕过证书链来验证信任。”来自msdn.microsoft.com/en-us/library/ms254500.aspx
标签: c# sql-server ssl sqlconnection