c# - 通过 C# 获取网站证书

标签 c# ssl cryptography

我可以用这段代码简单地显示证书。我的问题是如何在文件中存储或写入证书?

using System.Security;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;

//Do webrequest to get info on secure site
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://mail.google.com");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
response.Close();

//retrieve the ssl cert and assign it to an X509Certificate object
X509Certificate cert = request.ServicePoint.Certificate;

//convert the X509Certificate to an X509Certificate2 object by passing it into the constructor
X509Certificate2 cert2 = new X509Certificate2(cert);

string cn = cert2.GetIssuerName();
string cedate = cert2.GetExpirationDateString();
string cpub = cert2.GetPublicKeyString();

//display the cert dialog box
X509Certificate2UI.DisplayCertificate(cert2);

最佳答案

您可以调用 cert.Export(...) 来获取可以写入文件的 byte[]

关于c# - 通过 C# 获取网站证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29803506/

相关文章:

c# - 如果 Sum() 值返回 Null 使用 0,当前是否将其强制转换为可为 null 的 double 值?

html - rotativa 不在 SSL 上提供页眉或页脚

ios - 保护 iOS 应用程序中的图像

security - 数字签名与 HMAC 与通过 DH 的 key

c# - 如何在 .net core/Linux 中实现 Diffie Hellman

c# - 运行时的显式运算符转换

c# - 在 Windows Phone 8 中实现类似 Tab 的导航模型——怎么做?

c# - 模式匹配条件确保两个属性都不为空,但编译器会发出可能为空引用的警告。为什么?

ssl - 如何为 Amazon S3 存储桶配置 SSL

Delphi 5 Indy/ics SSL 解决方法?