Cast exception in Protocols.SAMLResponse.Assertions in Low-Level interface

I have this code:

Dim l_SAMLResponse As ComponentSpace.SAML2.Protocols.SAMLResponse
Dim l_SamlAssertion As ComponentSpace.SAML2.Assertions.SAMLAssertion = Nothing

Profiles.SSOBrowser.ServiceProvider.ReceiveSAMLResponseByHTTPPost(New HttpRequestWrapper(Request), SAMLresponse, relayState)

If l_SAMLResponse.Assertions().Count > 0 Then
   l_SamlAssertion = l_SAMLResponse.Assertions()(0)

Throws an exception with the message: Unable to cast object of type ‘System.Xml.XmlElement’ to type ‘ComponentSpace.SAML2.Assertions.SAMLAssertion’.

Since SAMLResponse.Assertions returns a list of SAMLAssertions, why would I get an exception indicating that it is a type of XmlElement?

Thanks.

SAMLResponse.Assertions returns a list which may contain unencrypted and unsigned assertions (SAMLAssertion), encrypted assertions (EncryptedAssertion), and signed assertions (XmlElement).

You’re seeing a signed SAML assertion being returned.

ServiceProvider.ReceiveSAMLResponseByHTTPPost is part of the SAML low-level API and we don’t recommend using this API unless you have a very specific use case requiring this.

It’s much better to call the SAML high-level API’s SAMLServiceProvider.ReceiveSSO as this will handle receiving and processing the SAML response and SAML assertion. This includes decrypting the SAML assertion, if required, and verifying any XML signatures, as well as various other security checks.

The example projects we include demonstrate calling the SAML high-level API.