c# - 获取ssl证书信息-.net

标签 c# ssl-certificate

我希望从任何给定的域名 SSL 证书中获取数据。例如,我想输入任何网站地址,例如“http://stackoverflow.com ”,我的代码将首先检查 SSL 证书是否存在。如果是这样,那么我希望它能提取证书的到期日期。 [我正在从数据库中读取域名]示例:http://www.digicert.com/help/

我需要创建一个网络服务来检查到期日期。我该如何实现? - 我查了很多不同的东西,比如 RequestCertificateValidationCallback 和 ClientCertificates 等。因为我是新手,所以我不确定必须做什么。

我可能完全错了(因此我需要帮助)但我会创建一个 HTTPWebRequest 然后以某种方式请求客户端证书和特定元素吗?

我尝试了提供的示例@SSL 证书预取 .NET,但我收到了被禁止的 403 错误。

非常感谢任何帮助 - 谢谢。

这是我编写的代码,它抛出 403 forbidden 错误。

        Uri u = new Uri("http://services.efi.com/");
        ServicePoint sp = ServicePointManager.FindServicePoint(u);

        string groupName = Guid.NewGuid().ToString();
        HttpWebRequest req = HttpWebRequest.Create(u) as HttpWebRequest;
        req.Accept = "*/*";
        req.ConnectionGroupName = groupName;

        using (WebResponse resp = req.GetResponse())
        {
            // Ignore response, and close the response.
        }
        sp.CloseConnectionGroup(groupName);

        // Implement favourite null check pattern here on sp.Certificate
        string expiryDate = sp.Certificate.GetExpirationDateString();

        string str = expiryDate;

最佳答案

这很好用:

namespace ConsoleApplication1
{
  using System;
  using System.Net;
  using System.Net.Security;
  using System.Security.Cryptography.X509Certificates;

  class Program
  {
    static void Main()
    {
      ServicePointManager.ServerCertificateValidationCallback += ServerCertificateValidationCallback;

      var request = WebRequest.Create("https://www.google.com");

      var response = request.GetResponse();

      Console.WriteLine("Done.");
      Console.ReadLine();

    }

    private static bool ServerCertificateValidationCallback(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
    {
      Console.WriteLine("Certificate expires on " + certificate.GetExpirationDateString());

      return true;
    }
  }
}

关于c# - 获取ssl证书信息-.net,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15335347/

相关文章:

jakarta-ee - Glassfish 3 和导入签名的 SSL 证书问题

apache - 我在哪里可以找到 CentOS 服务器中的 ssl.conf 文件?

c# - Panel.Dock Fill 忽略其他 Panel.Dock 设置

c# - 通过引用另一个表的列的计数(*)从一个表中获取数据

c# - Ghostscript 在拆分 PDF 后增加文件大小

ssl - curl 证书验证失败

java - IBM MQ8.0 - AMQ9503 channel 协商失败

ssl - 使用 Https 和第二个有效证书的中间人攻击

c# - Thread.Interrupt 在应用程序关闭时停止长时间 sleep - 是否有更好的方法

c# - 局部变量 C#