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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12337721/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 22:55:48  来源:igfitidea点击:

How to programmatically install a certificate using C#

c#visual-studiocertificatex509

提问by DroidBellmer

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#?

我的学校网页有自信任证书(你必须手动安装),我想创建一个程序,在我点击一个按钮后,将证书.cer(来自 Visual Studio 资源)安装到本地用户 -“受信任的根证书颁发机构”。知道如何在 Visual C# 中编码吗?

回答by akton

To add the certificate to the trusted root store for the current user programmatically, use the X509Storeand X509Certificate2classes. For example:

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

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();

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

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

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

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

certmgr /add cert.cer /s Root

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.

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