简体   繁体   中英

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. 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.

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

?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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