Pull SecurityToken from SAML statement

I have an XML SAML statement that looks like this:

<saml:Assertion MajorVersion="1" MinorVersion="1" AssertionID="_9b6e6302-d6a8-47f0-9155-1051a05edbfb" Issuer="http://example.com/adfs/services/trust" IssueInstant="2013-04-29T19:35:51.197Z" xmlns:saml="urn:oasis:names:tc:SAML:1.0:assertion"> ... </saml:Assertion> 

I am trying to get a SecurityToken from this XML using code similar to the following:

 // Loading the XML referenced above. XDocument doc = XDocument.Load(new StringReader(assertion)); // Creating config to use in TokenHandlers below; required if not using a SecurityTokenHandlerCollection. SecurityTokenHandlerConfiguration config = new SecurityTokenHandlerConfiguration(); config.AudienceRestriction.AllowedAudienceUris.Add(new Uri("https://localhost/Orchard/")); config.CertificateValidator = X509CertificateValidator.None; // Both of these lines throw Exceptions, as explained below. new Saml11SecurityTokenHandler() { Configuration = config }.ReadToken(doc.CreateReader()); new Saml2SecurityTokenHandler() { Configuration = config }.ReadToken(doc.CreateReader()); 

If I try to read the token using Saml11SecurityTokenHandler , I get the following exception:

ID4075: SAML assertion missing required MajorVersion attribute.

If I try to read the token using Saml2SecurityTokenHandler , I get another exception:

The Approval element with the namespace name "urn: oasis: names: tc: SAML: 2.0: assertion" was not found.

Obviously, for Saml2SecurityTokenHandler it makes sense, since this is a SAML 1.1 statement. However, why SAML 1.1 TokenHandler cannot read this statement?

EDIT : the reader appears empty; Why is this? doc has content.

 string notEmpty = doc.FirstNode.ToString(); string empty = doc.CreateReader().ReadOuterXml(); 
+4
source share
1 answer

Figure from the presented technique here , it works:

 SecurityToken token; using (StringReader sr = new StringReader(assertion)) { using (XmlReader reader = XmlReader.Create(sr)) { if (!reader.ReadToFollowing("saml:Assertion")) { throw new Exception("Assertion not found!"); } SecurityTokenHandlerCollection collection = SecurityTokenHandlerCollection.CreateDefaultSecurityTokenHandlerCollection(); token = collection.ReadToken(reader.ReadSubtree()); } } 

Make sure that you do not change the spaces in the XML document, otherwise you will receive a signature verification error.

+7
source

All Articles