title: How to tell if cert has private key
body: <p>I've got a keystore that is full of certificates. Another person has told me the certificates have a private key but when i check in my code i never see any. All of these keys were used in vs6 and cryptapi, i'm using c#. </p>
  
  <p>I created my own certificate and ensured it had a private key by using the methods here 
  <a href="http://www.source-code.biz/snippets/vbasic/3.htm" rel="nofollow">http://www.source-code.biz/snippets/vbasic/3.htm</a></p>
  
  <p>When i imported that into the same keystore and then ran my code that cert showed that it did indeed have a private key, all of the other certs in the key store dont (according to my code). </p>
  
  <p>Is there some flag or something i need to set in order to be able to use the private key within the certs in this keystore ? </p>
  
  <pre><code>public void testForPrivateKey(string keystorename)
  {
      X509Store teststore = new X509Store(keystorename);
  
      teststore.Open(OpenFlags.ReadOnly);
      foreach (X509Certificate2 cert in teststore.Certificates)
      {
          if(cert.HasPrivateKey)
          {
              System.Diagnostics.Debug.WriteLine("Private key found certificate name: {0}", cert.Subject);
          }
  
      }
      store.Close();
  
  }
  </code></pre>