title: Decode ASP Request.ClientCertificate("Certificate") from a .NET COM library
body: <p>In an effort to further refine my question this is the 3rd rewrite. Here is the scenario in a nutshell.</p>
  
  <ol>
  <li>Legacy ASP / VBscript website</li>
  <li>.NET / C# COM library which is used in the ASP site</li>
  <li><p>I want to send the Request.ClientCertificate("Certificate") in it's raw form to my .NET COM library and decode it so I can validate fields against the client certificate in Active Directory.</p>
  
  <ul>
  <li>The <a href="http://msdn.microsoft.com/en-us/library/ms524668%28v=vs.90%29.aspx" rel="nofollow">documentation</a> for Request.ClientCertificate("Certificate") says that it's a string of the binary data that represents the certificate in ASN.1 format. My .NET method that receives this has a string type for the parameter. I've tried using <strong>Encoding.UTF8.GetBytes</strong>, <strong>Encoding.Unicode.GetBytes</strong> and <strong>Encoding.ASCII.GetBytes</strong> to convert it to a byte[] that the <strong>X509Certificate2</strong> constructor expects.</li>
  </ul></li>
  </ol>
  
  <p>I have tried searching Google for every shred of information I can find on how to do something useful (ie. decode) the ASP Request.ClientCertificate in it's raw form but have come up empty. I've also saved this data to a file and attempted to decode this using CertUtil but it still fails to decode the data and just errors out.</p>
  
  <p>As I continue to play around with this I decided to look at the size of the client certificate returned by ASP and ASP.NET and I found the size to not match. My certificate coming from ASP was 647 bytes and 1295 bytes in ASP.NET. I don't know why the size is skewed between both environments. Consequently I have no issues whatsoever decoding the client certificate returned by ASP.NET using the X509Certicate2 class. </p>
  
  <p>I've also tried using the following p/invoked function:</p>
  
  <pre><code>[DllImport("crypt32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  public static extern uint CertCreateCertificateContext(uint dwCertEncodingType, 
    byte[] pbCertEncoded, uint cbCertEncoded);
  </code></pre>
  
  <p>So in either case of using X509Certificate, X509Certificate2 and the function above I get a consistent result the certificate coming from ASP is not usable by these functions. The X509Certificate class just throws an exception when I try to construct it with the byte[] representing the certificate.</p>
  
  <p>I've tried using the CAPICOM library to import the certificate but I get exactly the same exception that the X509Certificate2 class gives me. </p>
  
  <p><em>Cannot find the requested object</em></p>
  
  <p><strong>So in summary what I want to do is, decode the ASP Request.ClientCertificate("Certificate") from a .NET / C# COM library utilizing the X509Certificate set of classes.</strong></p>