Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 验证签名时SignedXml无法正常工作_.net_Casting_Verify_Xml Dsig_Signedxml - Fatal编程技术网

.net 验证签名时SignedXml无法正常工作

.net 验证签名时SignedXml无法正常工作,.net,casting,verify,xml-dsig,signedxml,.net,Casting,Verify,Xml Dsig,Signedxml,这是使用SignedXml类验证签名的一种方法,但在Visual Studio 2013的.NET版本4.0中,由于在代码中将XmlNode强制转换为XmlElement,因此该方法不适用于我: Boolean VerifyDetachedSignature( String^ XmlSigFileName ) { // Create a new XML document. XmlDocument^ xmlDocument = gcnew XmlDocument; // Lo

这是使用SignedXml类验证签名的一种方法,但在Visual Studio 2013的.NET版本4.0中,由于在代码中将XmlNode强制转换为XmlElement,因此该方法不适用于我:

Boolean VerifyDetachedSignature( String^ XmlSigFileName )
{

   // Create a new XML document.
   XmlDocument^ xmlDocument = gcnew XmlDocument;

   // Load the passed XML file into the document.
   xmlDocument->Load( XmlSigFileName );

   // Create a new SignedXMl object.
   SignedXml^ signedXml = gcnew SignedXml;

   // Find the "Signature" node and create a new 
   // XmlNodeList object.
   XmlNodeList^ nodeList = xmlDocument->GetElementsByTagName( "Signature" );

   // Load the signature node.
   signedXml->LoadXml( safe_cast<XmlElement^>(nodeList->Item( 0 )) );

   // Check the signature and return the result. 
   return signedXml->CheckSignature();
}
它在未找到引用文件时抛出异常,并返回一个真值,因为在找到所有引用文件时签名是正确的,这两种方法都是完美的

这意味着将XmlNode强制转换为XmlElement时会发生错误,即使它确实返回了我所需要的节点。我甚至试着用XmlWriter编写它,得到了我需要的东西


问题是,我需要以将XmlNode转换为XmlElement的方式进行转换,因为我正在处理的XML文件在树的更深处有一个签名元素,即,它不是根节点,我需要使用SelectSingleNode或GetItemByTagName。我怎样才能解决这个问题?

你有没有想过?我也看到了同样的问题。没有,但我找到了解决办法。如果我没有记错,我将节点保存到一个文件中,然后将该文件作为XmlDocument加载,然后执行CheckSignature。
   SignedXml^ signedXml = gcnew SignedXml(xmlDococument);
   signedXml->LoadXml(xmlDococument->DocumentElement);

   //xmlDococument->DocumentElement is an XmlElement