从Web浏览器控件打开证书信息

Kyl*_*ndo 4 .net c# browser ssl-certificate

有谁知道如何打开基于WebBrowser控件SSL的"证书信息"屏幕?

Yo *_*mma 8

这可以通过使用一个名为的类来实现X509Certificate2UI.

要使此类可用,您需要添加引用 System.Security.dll

X509Certificate2UI类中,你有一个meyhod调用DisplayCertificate(),它将一个X509Certificate2对象作为参数.调用时,此方法显示一个对话框,显示所有证书信息,包括链接,与您在IE中找到的对话框完全相同.

webbrowser控件只能返回一个X509Certificate然后可以传递给X509Certificate2类的构造函数的控件.

所以代码看起来像这样:

//includes on top
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://securesite.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);

//display the cert dialog box
X509Certificate2UI.DisplayCertificate(cert2);
Run Code Online (Sandbox Code Playgroud)