它听起来不符合逻辑,但是没有警告,您应该将证书添加到当前用户的根证书存储区,而不是添加到本地计算机的根目录。你可以很容易检验
certmgr.exe -add -c t.cer -s -r currentUser root
产生安全警告,但
certmgr.exe -add -c t.cer -s -r localMachine root
没有。
所以,如果你想导入.NET证书,则相应的代码可能是约以下
using System;
using System.Security.Cryptography.X509Certificates;
namespace AddCertToRootStore {
class Program {
static void Main (string[] args) {
X509Store store = new X509Store (StoreName.Root,
StoreLocation.LocalMachine);
store.Open (OpenFlags.ReadWrite);
X509Certificate2Collection collection = new X509Certificate2Collection();
X509Certificate2 cert = new X509Certificate2 (@"C:\Oleg\t.cer");
byte[] encodedCert = cert.GetRawCertData();
Console.WriteLine ("The certificate will be added to the Root...");
store.Add (cert);
Console.WriteLine("Verify, that the certificate are added successfully");
Console.ReadKey();
Console.WriteLine ("The certificate will be removed from the Root");
store.Remove (cert);
store.Close();
}
}
}
原因的Windows弹出这个问题的是,希望将证书添加到根存储带来了安全隐患,所以必须不要轻易做。我不确定是否有办法避免弹出窗口,但我希望不会!安装一个应用程序不应该被允许损害PC的安全性 - 特别是不会默默地。 – dajames 2010-11-20 14:13:54
@dajames Like Oleg在他的回答中写道:管理员可以在没有弹出窗口的情况下在本地机器可信根存储中安装证书。当你安装一个应用程序时,你通常会给安装程序管理员权限,所以我认为你的方案实际上是可行的。另一个原因,你应该小心哪些安装程序/应用程序,你给你宝贵的管理权限。 – Stefan 2011-09-28 16:53:23