有谁知道如何从 WebBrowser
控件打开基于 SSL 的“证书信息”屏幕?
最佳答案
这可以通过使用名为 X509Certificate2UI
的类来实现。
要使此类对您可用,您需要添加对 System.Security.dll
的引用
在 X509Certificate2UI
类中,您有一个名为 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);
关于c# - 从 Web 浏览器控制打开证书信息,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2690082/