Signed XML Check Signature is failing with hash Error

245 Views Asked by At

I am trying to check SAML Signed Response in Dot Net 4.8 MVC Project

My code is

_xmlDoc = new XmlDocument();
                _xmlDoc.PreserveWhitespace = true;
                _xmlDoc.XmlResolver = null;
                _xmlDoc.LoadXml(xml);

        XmlNodeList nodeList = _xmlDoc.SelectNodes("//ds:Signature", _xmlNameSpaceManager);

                SignedXml signedXml = new SignedXml(_xmlDoc);
                
                if (nodeList.Count == 0) return false;

                signedXml.LoadXml((XmlElement)nodeList[0]);

                X509Certificate2 serviceCertificate = null;
                foreach (KeyInfoClause clause in signedXml.KeyInfo)
                {
                    if (clause is KeyInfoX509Data)
                    {
                        if (((KeyInfoX509Data)clause).Certificates.Count > 0)
                        {
                            serviceCertificate = (X509Certificate2)((KeyInfoX509Data)clause).Certificates[0];
                        }
                    }
                }
return signedXml.CheckSignature(serviceCertificate, true);
 

Its getting false as signature validation

So I enabled logs and below is the error

[SignedXml#026c9e6f, VerifyReference] Reference Reference#000df9b5 hashed with 
"http://www.w3.org/2001/04/xmlenc#sha256" (SHA256Managed) has hash value 
348aa65d5b67edfae901cd4e2596065e75839a18c9a5d3a63d3fd85e48c232d8, expected hash value 
869e1b0273833271a23349cd9bc7bddb597c2277c323abddead0cfe798ae3f63.
System.Security.Cryptography.Xml.SignedXml Information: 12 : [SignedXml#026c9e6f, 
VerificationFailure] Verification failed checking references.

Please suggest

0

There are 0 best solutions below