【发布时间】:2018-09-05 19:55:57
【问题描述】:
我有一个 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: False
Chain error: RevocationStatusUnknown
Chain error: OfflineRevocation
所以当我这样做时:
ServicePointManager.CheckCertificateRevocationList = false;
ServicePointManager.ServerCertificateValidationCallback = (a, b, c, d) => true;
ServicePointManager.Expect100Continue = true;
Console.WriteLine("connessione a:" + host);
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(host);
req.PreAuthenticate = true;
req.AllowAutoRedirect = true;
req.ClientCertificates.Add(certificate);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
string postData = "login-form-type=cert";
byte[] postBytes = Encoding.UTF8.GetBytes(postData);
req.ContentLength = postBytes.Length;
Stream postStream = req.GetRequestStream();
postStream.Write(postBytes, 0, postBytes.Length);
postStream.Flush();
postStream.Close();
WebResponse resp = req.GetResponse();
服务器说证书未发送/无效。
我的问题是:
- 即使链构建错误,我如何发送证书?
- 还有另一个类发布证书,在发送之前不检查证书验证?
非常感谢。 安东尼诺
【问题讨论】:
-
Web 服务器是否信任您的客户端证书的颁发者?否则,Web 服务器将拒绝客户端证书。
-
是的,我忘了提到,如果我将证书添加到用户证书并尝试与 Internet Explorer 连接,则连接正常。
-
我使用包含私钥的 PKCS12 (.pfx) 证书测试了您的代码,结果成功
-
我的是 p12 文件。你能连接证书吗?它安装在用户证书密钥集中还是计算机密钥集中? (我有意大利语的窗口,我不确定条款)
-
是的,我能够连接到当前用户存储中安装的证书。由于它在 IE 中工作,请尝试将带有私钥的证书从 IE 导出到 .pfx Export Certificates (Windows),然后从您的代码中引用导出的 .pfx 文件。
标签: c# httpwebrequest x509certificate