如何以编程方式安装证书使用C#

问题描述:

我的学校网页有selftrusted证书(你必须手动安装),我想要创建程序,将安装一个certificate.cer(从visual studio资源)到本地用户 - 受信任的根证书颁发机构,我点击一个按钮。

my school webpages have selftrusted certificate(you must install it manually) and I wanted create program that will install a certificate.cer (from visual studio resources) to Local user -"Trusted root certificate authority" after i click on a button.Do you know how to code it in Visual C#?

将证书添加到当前用户的受信任根存储以编程方式使用 X509Store X509Certificate2 类。例如:

To add the certificate to the trusted root store for the current user programmatically, use the X509Store and X509Certificate2 classes. For example:

string file; // Contains name of certificate file
X509Store store = new X509Store(StoreName.Root, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadWrite);
store.Add(new X509Certificate2(X509Certificate2.CreateFromCertFile(file)));
store.Close();

另请参阅如何使用c#以编程方式将证书安装到本地机器存储?

See also " How can I install a certificate into the local machine store programmatically using c#? ".

另一个选项是证书管理器命令行(certmgr.exe)工具,具体为:

Another option is the Certificate Manager command line (certmgr.exe) tool, specifically:

certmgr /add cert.cer /s Root

其中cert.cer是您的证书。这将它导入到当前用户的受信任的根存储。但是,certmgr.exe是Visual Studio和Windows SDK的一部分,可能无法*分发。

where "cert.cer" is your certificate. This imports it into the trusted root store for the current user. However, certmgr.exe is part of Visual Studio and the Windows SDK and may not be freely distributable.