[英]C# get SSL Certificate into X509Certificate with “Forbidden” error
嘗試獲取一些SSL證書信息時出現問題。
通常,代碼可以正常工作,但是在某些情況下,我收到403 Forbidden錯誤。 即使發生這種情況,我也想獲取證書信息。 在很多情況下,我也會獲得無效的登錄憑據。
無論這些問題中的任何一個,我都希望獲取X509Certificate
對象。
如果我使用IE瀏覽它們,則無論消息如何,它都會為我提供證書信息。
因此,對於示例代碼,將request
和response
都定義為對象變量,並且在調用getRequest()
之前正確設置了request
。
如果沒有網絡錯誤,則此代碼可以正常工作,但是如果存在錯誤,則變量response
為null
。
是否還有另一種方法可以將X509Certificate
放入名為cert
的對象變量中,甚至看到這些錯誤?
老實說,我不在乎是否能夠響應,我只在乎X509Certificate
。
// callback used to validate the certificate in an SSL conversation
private static bool ValidateRemoteCertificate(
object sender,
X509Certificate certificate,
X509Chain chain,
SslPolicyErrors policyErrors
) {
// allow any old dodgy certificate...
log.write(0, "entering ValidateRemoeCertificate ");
return true;
}
// getRequest
private int getRequest()
{
try {
log.write(1, "Validating " + webHostName);
// request.Method = method;
ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback(ValidateRemoteCertificate);
response = (HttpWebResponse)request.GetResponse();
response.Close();
} catch (Exception w) {
if (w.Message.Contains("401"))
{
log.write(4, "Web Site Authentication Required for " + host + ":" + port);
return 401;
}
else if (w.Message.Contains("403"))
{
log.write(4, "Forbidden Access for " + host + ":" + port);
return 403;
}
else
{
log.write(4, "Error Reading Web Site " + host + " Port " + port + " " + w.Message);
return 400;
}
}
return 0;
}
感謝您的協助!
請改用SslStream類。 使用帶有RemoteCertificateValidationCallback委托的構造函數 ,該委托在AuthenticateAsClient期間被調用以檢查服務器的證書。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.