0
如何驗證.NET中HTTPS/SSL證書的有效性?如何驗證.NET中HTTPS/SSL證書的有效性?
理想情況下,我想建立一個HTTPS連接到網站,然後找出是否以有效的方式完成(證書未過期,主機名匹配,證書鏈信任等),但內置的HTTP客戶端似乎忽略證書錯誤(並且我不確定是否需要物理下載網頁來驗證證書?)。
我試着用下面的代碼(適應於在註釋的答案),但ValidationCallback不會被調用:
static void Main(string[] args)
{
String url = "https://www.example.com";
HttpWebRequest request = WebRequest.CreateHttp(url);
request.GetResponse();
request.ServerCertificateValidationCallback += ServerCertificateValidationCallback;
Console.WriteLine("End");
Console.ReadKey();
}
private static bool ServerCertificateValidationCallback(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
if (sslPolicyErrors == SslPolicyErrors.None)
{
Console.WriteLine("Certificate OK");
return true;
}
else
{
Console.WriteLine("Certificate ERROR");
return false;
}
}
可能重複[C#我如何驗證根CA證書(x509)鏈?](https://stackoverflow.com/questions/7331666/c-sharp-how-can-i-validate- a-root-ca-cert-certificate-x509-chain) – jAC
ServicePointManager是否設置爲忽略ssl驗證? [查看本文](https://stackoverflow.com/a/12507094/1709981)。 – guwere
@guwere好的開始,但它似乎不適合我?編輯了我的問題 – NickG