我有这样的源文件:
<LicenseFile xmlns="">
<Object Id="Settings">
<ProductID xmlns="">P2</ProductID>
<FirstName xmlns="">John</FirstName>
<LastName xmlns="">Jackson</LastName>
</Object>
</LicenseFile>
我需要添加符号并接收这样的文件:
<LicenseFile xmlns="">
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
...
<Object Id="Settings">
<ProductID xmlns="">P2</ProductID>
<FirstName xmlns="">John</FirstName>
<LastName xmlns="">Jackson</LastName>
</Object>
</Signature>
</LicenseFile>
但我有这样的结果文件:
<LicenseFile xmlns="">
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
...
</Signature>
<Object Id="Settings">
<ProductID xmlns="">P2</ProductID>
<FirstName xmlns="">John</FirstName>
<LastName xmlns="">Jackson</LastName>
</Object>
</LicenseFile>
我使用该代码:
XmlDocument doc = new XmlDocument();
doc.PreserveWhitespace = false;
doc.Load(new XmlTextReader(FileName));
SignedXml signedXml = new SignedXml(doc);
signedXml.SigningKey = Key;
Signature XMLSignature = signedXml.Signature;
Reference reference = new Reference("#Settings");
XmlDsigEnvelopedSignatureTransform env = new XmlDsigEnvelopedSignatureTransform();
reference.AddTransform(env);
XMLSignature.SignedInfo.AddReference(reference);
KeyInfo keyInfo = new KeyInfo();
keyInfo.AddClause(new RSAKeyValue((RSA)Key));
XMLSignature.KeyInfo = keyInfo;
// Compute the signature.
signedXml.ComputeSignature();
XmlElement xmlDigitalSignature = signedXml.GetXml();
// Append the element to the XML document.
doc.DocumentElement.AppendChild(doc.ImportNode(xmlDigitalSignature, true));
if (doc.FirstChild is XmlDeclaration)
{
doc.RemoveChild(doc.FirstChild);
}
// Save the signed XML document to a file specified
// using the passed string.
XmlTextWriter xmltw = new XmlTextWriter(SignedFileName, new UTF8Encoding(false));
doc.WriteTo(xmltw);
xmltw.Close();
什么是不正确的? 日Thnx。
答案 0 :(得分:1)
对象节点必须是签名的子节点,而不是同一级别。