我正在开展一个项目,我需要检查提供的XML
是否有效。
为此,我使用代码在c#中使用XML
类验证signedXml
。
请找到verify()
方法的以下代码:
public bool Verify(XmlDocument signedDocument)
{
// create a signed xml object from xml
SignedXml signedXml = new SignedXml(signedDocument);
// get the signature node and load the signature tag
XmlNodeList nodeList = signedDocument.GetElementsByTagName("Signature");
signedXml.LoadXml((XmlElement)nodeList[0]);
// define and create the encryption key
CspParameters CSPParam = new CspParameters();
CSPParam.Flags = CspProviderFlags.UseMachineKeyStore;
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(CSPParam);
rsa.FromXmlString(@"<RSAKeyValue><Modulus>03COUbx8JI6jkkmxrQ3N//67tgMt+ak1SKSXbGO3+4vVGFGhyGICJS8C+W6ON6dUdcm2/uxC5q4wyPRyFWMf8v8oslGOWJdADPnP8rvcy8PL3Nf67f8fMnTyHoEoZTfZjKuEoyhmPi6rfAX4QQA+OYhk4Qb1FJyOB3fpZ1QiTxX2k=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>");
// load encryption key to the xml
KeyInfo keyInfo = new KeyInfo();
keyInfo.AddClause(new RSAKeyValue(rsa));
signedXml.KeyInfo=keyInfo;
// check if the signature provided in signature tag is valid with the key info
bool b = signedXml.CheckSignature();
return b;
}
并且提供给该方法的xml数据是:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#" id="MySignature">
<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="#_ea559faf-417b-407f-bdc2-bccc76dab76c">
<Transforms>
<Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" />
<Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#">
</Transform>
</Transforms>
<DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
<DigestValue>fvQx+J90ZGKhwj8Mfhg6v/esOtI=</DigestValue>
</Reference>
</SignedInfo>
<SignatureValue>fvQx+J90ZGKhwj8Mfhg6v/esOtI=</SignatureValue>
<KeyInfo type="http://www.w3.org/2000/09/xmldsig#RSAKeyValue">
<RSAKeyValue><Modulus>03COUbx8JI6jkLrQ3N//67tgMt+ak1SKSXbGO3+4vVGFGhyGICJS8C+W6ON6dUdcm2/uxC5q4wyPRyFWMf8v8oslGOWJdADPnP8rvcy8PL3Nf67f8fMnTyHoEoZTfZjKuEoyhmPi6rfAX4QQA+OYhk4Qb1FJyOB3fpZ1QiTxX2k=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>
</KeyInfo >
</Signature>
</soap:Envelope>
我无法找出xml
中哪个标记值设置错误。
signedXml.CheckSignature()
的结果始终为false
。
有人可以帮我解决这个问题吗?
提前致谢。 如果需要更多信息,请告诉我。
答案 0 :(得分:0)
我删除了错误。从VS中的菜单:项目:添加新项:XML文件。然后将xml粘贴到页面中,确保在第一列中只有一个xml id行。寻找警告或错误。
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#" Id="MySignature">
<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="#_ea559faf-417b-407f-bdc2-bccc76dab76c">
<Transforms>
<Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" />
<Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#">
</Transform>
</Transforms>
<DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
<DigestValue>fvQx+J90ZGKhwj8Mfhg6v/esOtI=</DigestValue>
</Reference>
</SignedInfo>
<SignatureValue>fvQx+J90ZGKhwj8Mfhg6v/esOtI=</SignatureValue>
<KeyInfo xmlns:type="http://www.w3.org/2000/09/xmldsig#RSAKeyValue">
<KeyValue>
<RSAKeyValue>
<Modulus>03COUbx8JI6jkLrQ3N//67tgMt+ak1SKSXbGO3+4vVGFGhyGICJS8C+W6ON6dUdcm2/uxC5q4wyPRyFWMf8v8oslGOWJdADPnP8rvcy8PL3Nf67f8fMnTyHoEoZTfZjKuEoyhmPi6rfAX4QQA+OYhk4Qb1FJyOB3fpZ1QiTxX2k=</Modulus>
<Exponent>AQAB</Exponent>
</RSAKeyValue>
</KeyValue>
</KeyInfo >
</Signature>
</soap:Body>
</soap:Envelope>