title: How to programmatically import a pfx with a chain of certificates into the certificate store?
body: <p>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:</p>
  
  <ul>
  <li>Root certificate CA
  <ul>
  <li>Organization certificate CA<br>
  <ul>
  <li>Organization 2 certificate CA
  <ul>
  <li>My certificate</li>
  </ul></li>
  </ul></li>
  </ul></li>
  </ul>
  
  <p>The code I use looks like this:</p>
  
  <pre><code>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);
      }
  }
  </code></pre>
  
  <p>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:</p>
  
  <ul>
  <li>My certificate</li>
  </ul>
  
  <p>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?</p>