My Code:
private bool VerifyDataSignature(XmlDocument document)
{
bool result = true;
X509Certificate2 certificate;
XmlNodeList nodeList;
nodeList = document.GetElementsByTagName("509Certificate");
if (nodeList == null)
{
return false;
}
certificate = new X509Certificate2(Convert.FromBase64String(((XmlNode)nodeList[0]).InnerText));
if (certificate == null)
{
return false;
}
nodeList = document.GetElementsByTagName("Signature");
if (nodeList.Count <= 0)
{
throw new CryptographicException("Verification failed: No Signature was found in the document.");
}
if (nodeList.Count >= 2)
{
throw new CryptographicException("Verification failed: More that one signature was found for the document.");
}
SignedXml signedXml = new SignedXml(document);
signedXml.LoadXml((XmlElement)nodeList[0]);
try
{
result = signedXml.CheckSignature(certificate, false);
return result;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
return false;
}
}
Error Code:
Malformed reference element.
Can you tell me why and how to correct it? or can you give me a sample code if you think my code is wrong.
Attaching XML Signature element:
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
<SignedInfo>
<CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />
<SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" />
<Reference URI="#_518e1526-59d7-48e4-b1fb-a9a0887e2bcf">
<Transforms>
<Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" />
<Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" /><
/Transforms>
<DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
<DigestValue>7v4qo9SxATgIhk+VbDd0GeIbGA8=</DigestValue>
</Reference>
</SignedInfo>
<SignatureValue>
R4ey3rTZ9oKNUlk2TXN3XVChT0mdOdPa3Z92b+lXkPeJ3oqSfOi0Q3cZrnCXyE6+pdOQ9NsWy+wqoyctGl8x0uGxJdGN9zRw+FC5lFk/cQiLtsovYA73MC4vYKPoP32VZ3BedSyd0CP+nIKA8xIBLmFE12KUeJTS/O+0LCQyeC8HVpFd8d5T/pxy+XanMyJ00LRRT60gIlG9WibKmw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