title: How do I read a SAML response?
body: <p>I am doing a POC in ASP.Net for IdP Initiated SSO.
  I get logged in successfully with my AD credentials on ADFS login page.
  But, on my assertion page, how can I get the SAML response and validate the the user?
  I can see the response using developer tool but how can I get it using c#?</p>
  
  <p><strong>What I have tried:</strong></p>
  
  <p>I tried printing the values from SAMLResponse querystring parameter (I found this somewhere after googling. So not sure about how it actually works).</p>
  
  <pre><code>ClaimsPrincipal claimsPrincipal = System.Threading.Thread.CurrentPrincipal as ClaimsPrincipal;
  
  Response.Write("Is user Authenticated = " + claimsPrincipal.Identity.IsAuthenticated.ToString());
  </code></pre>
  
  <p><strong>I get this as : False</strong></p>
  
  <pre><code>Response.Write("   Current Principal  = " + System.Threading.Thread.CurrentPrincipal.ToString());
  </code></pre>
  
  <p><strong>I get this as : System.Security.Principal.GenericPrincipal</strong></p>
  
  <pre><code>string rawSamlData = Request["SAMLResponse"];
  Response.Write("Raw data \n");
  Response.Write(rawSamlData);
  
  rawSamlData = HttpUtility.UrlDecode(rawSamlData);
  Response.Write("after url decode \n");
  Response.Write(rawSamlData);
  
  // read the base64 encoded bytes
  byte[] samlData = Convert.FromBase64String(rawSamlData);
  Response.Write("after base 64 \n");
  Response.Write(samlData);
  
  // read back into a UTF string
  string samlAssertion = Encoding.UTF8.GetString(samlData);
  Response.Write("saml assertion \n");
  Response.Write(samlAssertion);
  </code></pre>
  
  <p>All I get is some encrypted string. How can I decode it to SAML response and authenticate the user? </p>