Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7436727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:17:12+00:00 2026-05-29T10:17:12+00:00

I am trying to programmatically import a X509 certificate (pfx / PKCS#12) in my

  • 0

I am trying to programmatically import a X509 certificate (pfx / PKCS#12) in my local machine’s certificate store. This particular certificate has a chain of certificates, the certification path looks something like this:

  • Root certificate CA
    • Organization certificate CA
      • Organization 2 certificate CA
        • My certificate

The code I use looks like this:

cert = new X509Certificate2(pathToCert, password);

if (cert != null)
{
    var store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
    store.Open(OpenFlags.ReadWrite);
    if (!store.Certificates.Contains(cert))
    {
        store.Add(cert);
    }
}

This code does import the certificate, however it seems to ignore the chain. If I check the certificate in the store, the certification path only shows:

  • My certificate

However when I import the pfx manually, it does show the full path.
Am I skipping a step here, or am I missing some parameter? Can someone shed some light on this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T10:17:13+00:00Added an answer on May 29, 2026 at 10:17 am

    You should be able to iterate over the certs in your PFX (and import each into the cert store of your choice) by opening the PFX file as an X509Certificate2Collection object.

    Here are the docs on X509Certificate2Collection:

    http://msdn.microsoft.com/en-us/library/system.security.cryptography.x509certificates.x509certificate2collection.aspx

    MSDN provides some sample code in that docs page on how to inspect each cert in the collection.

    Once you know the CNs/Issuers/other info about each cert it should be clear which certificate store each one needs to be added to. For that you can use the X509Store class and the StoreName enumeration to specify which store you want to open/add to:

    http://msdn.microsoft.com/en-us/library/system.security.cryptography.x509certificates.x509store.aspx

    http://msdn.microsoft.com/en-us/library/system.security.cryptography.x509certificates.storename.aspx

    Also see my answer to a similar SO question:

    How to retrieve certificates from a pfx file with c#?

    As mentioned in one of the latest comments on that answer, when you try to import a cert to the current user’s Root store (“StoreName.Root” and “StoreLocation.CurrentUser” as the name/location) you will get a popup dialog asking you to confirm.

    To solve that I just added a little MS UI Automation code to my cert import method, to click OK on the prompt.

    Or, as the commenter “CodeWarrior” says in the other SO answer’s comment, to avoid the popup dialog you can try putting the root cert into the LocalMachine store instead of CurrentUser.

    Sample code:

    string certPath = <YOUR PFX FILE PATH>;
    string certPass = <YOUR PASSWORD>;
    
    // Create a collection object and populate it using the PFX file
    X509Certificate2Collection collection = new X509Certificate2Collection();
    collection.Import(certPath, certPass, X509KeyStorageFlags.PersistKeySet);
    
    foreach (X509Certificate2 cert in collection)
    {
        Console.WriteLine("Subject is: '{0}'", cert.Subject);
        Console.WriteLine("Issuer is:  '{0}'", cert.Issuer);
    
        // Import the certificate into an X509Store object
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to programmatically switch tabs. I tried to do this using the following
I'm trying to programmatically dial a phone number on iPhone by running this command:
I'm trying to programmatically start explorer.exe but I'm not having any luck. This is
Trying to programmatically add options to a SELECT drop down in IE Windows Mobile.
I was trying to programmatically go through presidential campaign contributions to see which web
I'm trying to programmatically set the constructor sting of a COM+ component from a
I'm trying to programmatically add an Execute SQL task from within a script task
I am trying to programmatically create a PowerPoint from graphs in Access. Ideally, when
I am trying to programmatically drop a column in a table inside of an
I'm trying to programmatically extract information from an Enterprise Architect model (saved in an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.