简体   繁体   English

无法将证书添加到 X509Store

[英]Unable to Add certificate to X509Store

Im trying to add a certificate to a certificate store programatically I'm using the following code to fetch a pfx file from a directory and add the certificate to the CurrentUser under My store.我试图以编程方式将证书添加到证书存储我正在使用以下代码从目录中获取 pfx 文件并将证书添加到我的存储下的 CurrentUser。 The code runs without any exception but I'm not able to see the certificate added in the store.代码运行无任何异常,但我无法在商店中看到添加的证书。 I've tried changing CurrentUser to LocalMachine and have also tried adding under TrustedPeople but with no success.我已经尝试将 CurrentUser 更改为 LocalMachine 并尝试在 TrustedPeople 下添加但没有成功。

X509Certificate2 cer = new X509Certificate2(Server.MapPath("<filepath>"), "<pswd>", X509KeyStorageFlags.MachineKeySet );
                StorePermission sp =
            new StorePermission(PermissionState.Unrestricted);
            sp.Flags = StorePermissionFlags.AllFlags;
            sp.Assert();
            X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
            store.Open(OpenFlags.MaxAllowed);
            store.Certificates.Add(cer);
        store.Close();

Have your tried有没有试过

store.Add(cer);

instead of代替

store.Certificates.Add(cer);

? ?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM