【问题标题】:how to obtain SSL certificate information of a remote server in c#c#中如何获取远程服务器的SSL证书信息
【发布时间】:2013-03-18 20:35:51
【问题描述】:

我必须在 c# 中开发一个应用程序来获取 SSL 证书信息,如到期日期、颁发者等基于我提供的 DNS(比如 *.google.com),以便如果到期日期临近,我可以主动处理它.如果我将 DNS 提供为 *.google.com,那么我需要获取该域的 SSL 证书信息的详细信息。

我尝试关注http://awesomeideas.net/page/Cert-Expiry-Check.aspx,但我觉得这是针对存储在本地系统中的证书。我也尝试使用 HttpWebRequest 来获取 SSL 证书的详细信息,但它要求我输入一个有效的 URI,在我的情况下这是不可用的。我只有 DNS 名称

下面是我使用 HttpWebRequest 获取信息的代码。但它要求我输入 https://*.domain.com 类型的有效 URI

Uri uri = new Uri(DNSEntry); 
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri); 
request.Method = WebRequestMethods.Http.Get; 
HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 
X509Certificate cert1 = request.ServicePoint.Certificate; 
X509Certificate2 cert = new X509Certificate2(cert1); 
DateTime dtCertExpiry = Convert.ToDateTime(cert.NotAfter.ToString());

【问题讨论】:

  • 很抱歉没有正确发布问题。我必须在 C# 中开发一个应用程序来获取 SSL 证书信息,如到期日期,由等基于我提供的 DNS [说 *.google.com] 颁发,以便如果到期日期临近,我可以主动处理它。如果我将 DNS 提供为 *.google.com,那么我需要获取该域的 SSL 证书信息的详细信息
  • 您尝试过什么来实现这一目标?您是否需要大体上的方向,或者您正在做的事情有困难?
  • 这是我第一次使用 SSL。所以我尝试关注awesomeideas.net/page/Cert-Expiry-Check.aspx,但我觉得这是针对存储在本地系统中的证书。我也尝试使用 HttpWebRequest 来获取 SSL 证书的详细信息,但它要求我输入一个有效的 URI,在我的情况下这是不可用的。我只有 DNS 名称
  • 下面是我使用HttpWebRequest获取信息的代码。但它要求我输入类型为 https://*.domain.com Uri uri = new Uri(DNSEntry); 的有效 URI HttpWebRequest 请求 = (HttpWebRequest)WebRequest.Create(uri); request.Method = WebRequestMethods.Http.Get; HttpWebResponse 响应 = (HttpWebResponse)request.GetResponse(); X509Certificate cert1 = request.ServicePoint.Certificate; X509Certificate2 证书 = 新 X509Certificate2(cert1); DateTime dtCertExpiry = Convert.ToDateTime(cert.NotAfter.ToString());

标签: c# ssl ssl-certificate


【解决方案1】:

我尝试使用以下它工作正常:

string strDNSEntry 是您需要 SSL 的 DNS

public X509Certificate2 DownloadSslCertificate(string strDNSEntry)
{

    X509Certificate2 cert = null;
    using (TcpClient client = new TcpClient())
    {
        //ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;           
        client.Connect(strDNSEntry, 443);

        SslStream ssl = new SslStream(client.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null);
        try
        {
            ssl.AuthenticateAsClient(strDNSEntry);
        }
        catch (AuthenticationException e)
        {
            log.Debug(e.Message);
            ssl.Close();
            client.Close();
            return cert;
        }
        catch (Exception e)
        {
            log.Debug(e.Message);
            ssl.Close();
            client.Close();
            return cert;
        }
        cert = new X509Certificate2(ssl.RemoteCertificate);
        ssl.Close();
        client.Close();
        return cert;
    }
}


public static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
    if (sslPolicyErrors == SslPolicyErrors.None)
        return true;

    Console.WriteLine("Certificate error: {0}", sslPolicyErrors);

    // Do not allow this client to communicate with unauthenticated servers. 
    return false;
}

【讨论】:

    猜你喜欢
    • 2018-02-04
    • 2018-02-26
    • 1970-01-01
    • 1970-01-01
    • 2018-07-07
    • 1970-01-01
    • 2011-12-13
    • 2015-09-25
    • 2015-08-10
    相关资源
    最近更新 更多