Having a weird issue. I have a SAML token with an encrypted assertion. It parses XML just fine. I can load it into SAMLResponse just fine. I can pull off the EncryptedAssertion using SAMLResponse.GetEncryptedAssertion() just fine. However when I call EncryptedAssertion.Decrypt() I get an exception: Exception: ComponentSpace.SAML2.Exceptions.SAMLEncryptionException Message: Failed to decrypt XML.
Inner Exception: { Exception: System.Xml.XmlException Message: 'saml' is an undeclared prefix. Line 1, position 2. Stack Trace at System.Xml.XmlTextReaderImpl.Throw(Exception e) at System.Xml.XmlTextReaderImpl.Throw(String res, String arg, Int32 lineNo, Int32 linePos) at System.Xml.XmlTextReaderImpl.LookupNamespace(NodeData node) at System.Xml.XmlTextReaderImpl.ElementNamespaceLookup() at System.Xml.XmlTextReaderImpl.ParseAttributes() at System.Xml.XmlTextReaderImpl.ParseElement() at System.Xml.XmlTextReaderImpl.ParseDocumentContent() at System.Xml.XmlTextReaderImpl.Read() at System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean preserveWhitespace) at System.Xml.Xmldocument.Load(XmlReader reader) at ComponentSpace.SAML2.Utility.Xml.LoadDocument(String xmlText) at ComponentSpace.SAML2.Utility.XmlEncryption.Decrypt(XmlElement encryptedElement, XmlNodeList encryptedKeysNodeList, AsymmetricAlgorithm keyDecryptingKey, EncryptionMethod keyEncryptionMethod, EncryptionMethod dataEncryptionMethod)
} }I did a ToXml().OuterXml on the EncryptedAssertion and sure enough the prefix is defined: <saml:EncryptedAssertion xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion"> I then did an Xmldocument.LoadXml() on the results of EncryptedAssertion.ToXml().OuterXml, and it loads just fine. I've attached the token and the pfx file Base64 encrypted (Since pfx files aren't allowed to be attached here) You can execute and re-create with the following code: var xml = File.ReadAllText("workingEncrypted.xml"); var doc = new XmlDocument(); doc.PreserveWhitespace = true; doc.LoadXml(xml);
var pfx = File.ReadAllText("qa.pfx.txt");//base64 of the pfx file var pfxBytes = Convert.FromBase64String(pfx); var cert = new X509Certificate2(pfxBytes);
var response = new SAMLResponse(doc.DocumentElement); var encryptedAssertion = response.GetEncryptedAssertion(); var assertion = encryptedAssertion.Decrypt(cert);//This line blows up. Any ideas why I'm getting that exception? Thanks!
|