如何以编程方式安装证书使用C# [英] How to programmatically install a certificate using C#

查看:694
本文介绍了如何以编程方式安装证书使用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.

这篇关于如何以编程方式安装证书使用C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆