Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# X509Store.Add/Remove操作系统同步_C#_Windows_Cryptography_Certificate_X509certificate - Fatal编程技术网

C# X509Store.Add/Remove操作系统同步

C# X509Store.Add/Remove操作系统同步,c#,windows,cryptography,certificate,x509certificate,C#,Windows,Cryptography,Certificate,X509certificate,我正在尝试将证书添加到当前用户证书存储中,并在它从其他应用程序读取证书后立即添加。所以,我现在做的是 var cert = new X509Certificate2(...//binary certificate here){ PrivateKey = myPrivateKey }; var store = new X509Store(StoreName.My, StoreLocation.CurrentUser); store.Open(OpenFlags.MaxAllowed); st

我正在尝试将证书添加到当前用户证书存储中,并在它从其他应用程序读取证书后立即添加。所以,我现在做的是

var cert = new X509Certificate2(...//binary certificate here){
  PrivateKey = myPrivateKey
};

var store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.MaxAllowed);
store.Add(new X509Certificate2(cert.Export(X509ContentType.Pfx), string.Empty, X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.UserKeySet));
store.Close();
store = null;

cert.Reset();
cert = null;
此操作完成后,应用程序仍处于打开状态并执行其他操作,但并非总是能够在存储中找到此证书。 但是,如果我们等待1/2分钟,我们肯定会找到此证书


怎么了?X509Store.Add是否可能不是同步操作,可能在关闭并处理完之后,Windows仍在执行一些操作来完成导入过程?

今天这里出现了同样的问题,您解决了吗?@Leonardocoper是,最快的解决方案是将密钥导出到pfx文件中,然后从文件本身而不是从其内容加载并创建证书。另一种解决方案是完全清除存储中的所有相关证书,然后像往常一样导入它。我不知道这些方法为什么有效,但它们确实有效。