JG Vimalan's Blog

It is all about coding!

System.Security.Cryptography.CryptographicException: Malformed reference element

I got this error when I tried to verify the SAML 2.0 signature. I was using .NET 3.5 webapplication to consume the SAML 2.0 response and then tried to verify the signature. Inorder to fix this error all you need to do is, set the correct XmlElement while performing the signedXml.LoadXml() method. Here is the code snippet I used for fixing the issue.

XmlDocument samlDoc = new XmlDocument();
samlDoc.PreserveWhitespace = true;
samlDoc.LoadXml(decodedSamlResponse);

var nSpace = new XmlNamespaceManager(samlDoc.NameTable);
nSpace.AddNamespace(“saml2p”, @”urn:oasis:names:tc:SAML:2.0:protocol”);
nSpace.AddNamespace(“saml2″, @”urn:oasis:names:tc:SAML:2.0:assertion”);
nSpace.AddNamespace(“ds”, SignedXml.XmlDsigNamespaceUrl);

var signNode = samlDoc.SelectSingleNode(“/saml2p:Response/ds:Signature”, nSpace);

var signedXml = new SignedXml(samlDoc.DocumentElement);
signedXml.LoadXml(signNode as XmlElement);

if (signedXml.CheckSignature(pubCert, true))
{
//read the saml data
}

 

Advertisements

June 10, 2011 Posted by | ASP.NET, C#.NET, SAML 2.0 | 1 Comment