title: Signing Credentials - X509 Certificate with Private Key - SAML2
body: <p>I know hardly anything about certificates, with regards that there's a <code>CA</code>, <code>public</code> and <code>private key</code>, and I'm learning as I go. I am creating an SSO login using <code>SAML 2</code>, which I have working with the excecption of adding the <code>&lt;Signature&gt;</code> element.</p>
  
  <p><strong>Creating the certificate:</strong></p>
  
  <p>I have a copy of <code>makecert.exe</code> and <code>pvk2pfx.exe</code> in a directory. I open <code>cmd</code> and type the following:</p>
  
  <pre><code>makecert -r -pe -n "CN=Test Cert" -sky exchange -sv testcert.pvk testcert.cer
  </code></pre>
  
  <p>A dialog box pops up, asking for a password and confirm password, which I enter. Another dialog box pops up asking for the password (I'm assuming this is the same one I entered prior, which I have been doing). This creates a <code>testcert.cer</code> in the same directory. </p>
  
  <p>I then type this into <code>cmd</code></p>
  
  <pre><code>pvk2pfx -pvk testcert.pvk -spc testcert.cer -pfx testcert.pfx
  </code></pre>
  
  <p><strong>EDIT:</strong> It asks me for a password. I enter the same password (<code>private key</code>) that I used when creating the <code>cer</code>.</p>
  
  <p>It creates a <code>pfx</code> file in the directory. </p>
  
  <hr>
  
  <p>Here's where I am confused. If I import the <code>cer</code> file into <code>MMC</code>, I can access it: </p>
  
  <pre><code>        X509Certificate2 cert = null;
  
          var store = new X509Store(StoreLocation.CurrentUser);
          store.Open(OpenFlags.ReadOnly);
  
          var storeCollection = store.Certificates.Find(X509FindType.FindBySubjectName, "Test Cert", false);
          if (storeCollection.Count == 1)
          {
              cert = storeCollection[0];
          }
  
          if (cert == null)
          {
              throw new ArgumentNullException("Certificate", "No certificate found.");
          }
  
          store.Close();
  </code></pre>
  
  <p>However, when I do this, the private key (property) is <code>null</code>. I read that the private key is in the <code>pfx</code> file. So, instead of accessing the store, I do this:</p>
  
  <p><code>privateKey</code> is the private key that I used when creating the certificate using <code>makecert.exe</code></p>
  
  <pre><code>X509SigningCredentials clientSigningCredentials = new X509SigningCredentials(
       X509Certificate2(@"c:\directory\testcert.pfx", privateKey, X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.Exportable));
  </code></pre>
  
  <p>I receive an error: <code>The specified network password is not correct.</code></p>
  
  <p>I also attempt to import the <code>pfx</code> file into <code>MMC</code>, instead of the <code>cer</code> file. It asks me for a password. I put the password in that I used to create the certificate using <code>makecert.exe</code>. It <strong>ALWAYS</strong> tells me that the password is incorrect. </p>
  
  <p>What am I doing wrong?</p>