Tag: httpwebrequest x509certificate

强制HttpWebRequest发送客户端证书

我有一个p12证书,我以这种方式加载它: X509Certificate2 certificate = new X509Certificate2(certName, password, X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable); 它是正确加载的,实际上如果我做了certificate.PrivateKey.ToXmlString(true); 它返回一个完整的xml而没有错误。 但如果我这样做: try { X509Chain chain = new X509Chain(); var chainBuilt = chain.Build(certificate); Console.WriteLine(“Chain building status: “+ chainBuilt); if (chainBuilt == false) foreach (X509ChainStatus chainStatus in chain.ChainStatus) Console.WriteLine(“Chain error: “+ chainStatus.Status); } catch (Exception ex) { Console.WriteLine(ex); } 它写道: Chain building status: […]

调试失败的HTTPS WebRequest

我正在编写一个小程序,它将使用HTTPS和HttpWebRequest类向服务器发出GET请求。 服务器(显然)有一个服务器证书。 它还希望客户提供证书。 但是,在发出请求时,我收到一个System.Net.WebException,指出无法建立安全的TLS / SSL连接。 我很快发现服务器的证书无效。 假设这是造成exception的原因,我试图接受无效证书(遗憾的是,更新证书不是一个选项),使用下面的代码: ServicePointManager.ServerCertificateValidationCallback += delegate { return true; }; 然而,这并没有解决问题。 由于exception没有给出任何细节,因此很难确定导致它的原因。 我尝试覆盖无效的服务器证书是否无效? 我提供的客户端证书是否不受服务器信任? 我没有以正确的方式加载客户端证书吗? 我喜欢有关如何调试此类问题的提示。 遗憾的是,我无法访问服务器或其日志。 以下是代码的重要部分: ServicePointManager.ServerCertificateValidationCallback += delegate { return true; }; HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); // url is an HTTPS URL. X509Certificate clientCert = new X509Certificate(“certificate.crt”, “password”); req.ClientCertificates.Add(clientCert); WebResponse resp = req.GetResponse(); // This fails!