none
Exporting Certificates IN C#

    Question

  • HI,

    I am trying to Export Certificates from Current User and Local machine. It executes Perfectly.Below is the code.


    But Sometimes, I see in Some of the Servers Under Current User Certificate Count doesn't match.

    For example I see under Root 45 Certificates, i get only 42. I also checked the count for them it will be only 42 in the Store Location. Can you Please help me with the above code why am i Missing them?

    Thanks,

    Sujith.


    Sujith


    Thursday, March 30, 2017 5:35 PM

Answers

  • Hi sujith reddy komma,

    Thank you for posting here.

    For your question, maybe the 3 certificates in the Store Location are non-exportable certificate. Normally, we could not export non-exportable certificates. I pick up an article about exporting this type of certificates, please refer to the following link for details.

    http://stackoverflow.com/questions/3914882/how-to-export-non-exportable-private-key-from-store

    And which Root Certificates folder you get the 45? For local machine certificate store or current user certificate store?

    Here is the article about certificate store in local machine or current user for your reference.

    For example, if a certificate is added to the local machine Trusted Root Certification Authorities certificate store, all current user Trusted Root Certification Authorities certificate stores also contain the certificate. 

    I hope this would be helpful.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Friday, March 31, 2017 6:47 AM
    Moderator

All replies

  • HI,

    I am trying to Export Certificates from Current User and Local machine. It executes Perfectly.Below is the code.

    try
                {
                    int i = 0;
                    foreach (string iStoreLo in Enum.GetNames(typeof(StoreLocation)))
                    {
                        foreach (string iStoreNa in Enum.GetNames(typeof(StoreName)))
                        {
                            StoreLocation storeLoc = (StoreLocation)Enum.Parse(typeof(StoreLocation), iStoreLo);
                            StoreName storeNam = (StoreName)Enum.Parse(typeof(StoreName), iStoreNa);
    
                            store = new X509Store(storeNam, storeLoc);
    
                            try
                            {
                                store.Open(OpenFlags.ReadOnly);
                                Console.WriteLine( store.Certificates.Count);
                                foreach (X509Certificate2 certificate in store.Certificates)
                                {
                                    try
                                    {
                                        thumbPrint = certificate.Thumbprint;
                                       // Console.WriteLine ("File Name: " + certPath + @"\" + iStoreLocation + "_" + iStoreName + "_" + thumbPrint + "_" + i.ToString());
                                        if (certificate.HasPrivateKey)
                                        {
                                            certBytes = certificate.Export(X509ContentType.Pfx, "Password");
                                            certFileName = certPath + @"\" + iStoreLo + "_" + iStoreNa + "_" + thumbPrint + "_" + i.ToString() + ".PFX";
                                            File.WriteAllBytes(certFileName, certBytes);
                                        }
                                        else
                                        {
                                            certBytes = certificate.Export(X509ContentType.Cert);
                                            certFileName = certPath + @"\" + iStoreLo + "_" + iStoreNa + "_" + thumbPrint + "_" + i.ToString() + ".CER";
                                            File.WriteAllBytes(certFileName, certBytes);
                                        }
    
                                    }
                                    catch (Exception ex)
                                    {
                                        Console.WriteLine ("File Name: " + certPath + @"\" + iStoreLocation + "_" + iStoreName + "_" + thumbPrint + "_" + i.ToString());
                                        Console.WriteLine("Exception in Cert: " + certFileName);
                                        Console.WriteLine(ex);
                                    }
                                    finally
                                    {
                                        i++;
                                    }
                                }
    
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine(ex);
                            }
                            finally
                            {
                                store.Close();
                            }
                        }
                    }

    But Sometimes, I see in Some of the Servers Under Current User Certificate Count doesn't match.

    For example I see under Root 45 Certificates, i get only 42. I also checked the count for them it will be only 42 in the Store Location. Can you Please help me with the above code why am i Missing them?

    Thanks,

    Sujith.


    Sujith

    Thursday, March 30, 2017 5:38 PM
  • Hi Sujith,

    Thanks for posting here.

    This forum is about Visual Studio General Questions, As your issue is related to C# programming language forum, I help you move this case to C# programming language forumfor suitable support.

    Thanks for your understanding and cooperation.


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, March 31, 2017 2:11 AM
  • Hi sujith reddy komma,

    Thank you for posting here.

    For your question, maybe the 3 certificates in the Store Location are non-exportable certificate. Normally, we could not export non-exportable certificates. I pick up an article about exporting this type of certificates, please refer to the following link for details.

    http://stackoverflow.com/questions/3914882/how-to-export-non-exportable-private-key-from-store

    And which Root Certificates folder you get the 45? For local machine certificate store or current user certificate store?

    Here is the article about certificate store in local machine or current user for your reference.

    For example, if a certificate is added to the local machine Trusted Root Certification Authorities certificate store, all current user Trusted Root Certification Authorities certificate stores also contain the certificate. 

    I hope this would be helpful.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Friday, March 31, 2017 6:47 AM
    Moderator