title: How to parse a SAML assertion request in .Net
body: <p>I'm trying to implement a SAML SSO solution in .Net, but I'm having a problem parsing the assertion.</p>
  
  <p>I have a sample assertion (looks like <code>byte[]</code> data as text) and corresponding <code>.p7b</code> file.</p>
  
  <p>I want to load the keys from the <code>.p7b</code> and decrypt the assertion to an XML document.</p>
  
  <p>So far I think I'm reading the keys correctly:</p>
  
  <pre><code>// get the key data
  byte[] certificateData = System.IO.File.ReadAllBytes("myKeys.p7b");
  
  // decode the keys
  var cms = new SignedCms(SubjectIdentifierType.IssuerAndSerialNumber);
  cms.Decode(certificateData);
  
  var samlCertificates = cms.Certificates;
  </code></pre>
  
  <p>Then I try to parse the assertion I get a problem:</p>
  
  <pre><code>// we have a keychain of X509Certificate2s, we need a collection of tokens
  var certificatesAsTokens =
      from X509Certificate2 cert in samlCertificates
      select new X509SecurityToken(cert) as SecurityToken;
  
  // get a token resolver
  var tokens = new ReadOnlyCollection&lt;SecurityToken&gt;(
      certificatesAsTokens.ToList());
  var resolver = SecurityTokenResolver.CreateDefaultSecurityTokenResolver(
      tokens, true);
  
  // get the SAML data in an XML reader
  var reader = XmlReader.Create(assertionPostStream);
  
  // use the WS Security stuff to parse the reader
  var securityToken = WSSecurityTokenSerializer.
      DefaultInstance.ReadToken(reader, resolver) as SamlSecurityToken;
  </code></pre>
  
  <p>That last statement throws an exception, stating that it can't parse the XML content.</p>
  
  <p>I think this means that I'm missing a step decrypting the assertion - getting the <code>byte[]</code> as text converted to a SAML format XML document.</p>
  
  <p>Anyone know how to add this step? Am I missing something else?</p>