将证书添加到 x509Store 不会执行任何 C# 操作

本文关键字:执行 任何 操作 证书 添加 x509Store | 更新日期: 2023-09-27 18:34:11

我正在尝试添加证书,但 Add 函数似乎没有任何作用。

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

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

将证书添加到 x509Store 不会执行任何 C# 操作

我让它工作了...事实证明,您应该使用商店。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();

尝试使用此标志:商店。Open (OpenFlags.ReadWrite(;

http://msdn.microsoft.com/en-us/library/system.security.cryptography.x509certificates.openflags(v=vs.110(.aspx