【问题标题】:Adding a Certificate to x509Store doesn't do anything C#向 x509Store 添加证书不会做任何事情 C#
【发布时间】:2015-02-11 03:44:18
【问题描述】:

我正在尝试添加证书,但添加功能似乎没有做任何事情。

我有两个证书。我都可以通过右键单击并保存到个人“testStore”商店来手动添加,但是当我尝试以编程方式添加它们时它们不会被保存。我什至只添加了其中一个,X509Store 对象按预期包含它,但是当我调用 .Add(cert) 时,那里没有保存任何内容。

//I've already added 1 cert manually
X509Certificate2 cert2 = new X509Certificate2(@"C:\temp\Cert2.cer");
X509Store store = new X509Store("testStore", StoreLocation.CurrentUser);
store.Open(OpenFlags.MaxAllowed);

//here store.Certificates has the one Certificate I added manually as expected.

store.Certificates.Add(cert2);

//here store.Certificates still only has the first certificate, cert2 still isn't there..

store.Close();

我错过了什么吗?

编辑 我也尝试过使用 StorePermission(如下所示)并尝试模拟管理员帐户,但这些都没有帮助

StorePermission sp = new StorePermission( PermissionState.Unrestricted);
sp.Flags = StorePermissionFlags.AllFlags;
sp.Assert();

【问题讨论】:

    标签: c# x509certificate x509 certificate-store


    【解决方案1】:

    我让它工作了......事实证明你应该使用 store.Add() 而不是 store.Certificates.Insert();

    //When LocalMachine is used, .Add() requires that you run the app as an administrator in order to work.
    X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
    X509Certificate2 cert = new X509Certificate2("C:\\test\\test.cer");
    store.Open(OpenFlags.MaxAllowed);
    store.Add(cert);
    store.Close();
    

    【讨论】:

      【解决方案2】:
      猜你喜欢
      • 2015-08-10
      • 1970-01-01
      • 2018-12-25
      • 2013-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-24
      • 2013-04-26
      相关资源
      最近更新 更多