不正确的引用元素签名XML C#
我需要实施EBICS协议,特别是HPB请求,我需要签署我的XML文件:不正确的引用元素签名XML C#,c#,xml,signature,xml-signature,C#,Xml,Signature,Xml Signature,我需要实施EBICS协议,特别是HPB请求,我需要签署我的XML文件: <?xml version="1.0" encoding="UTF-8"?> <ebicsNoPubKeyDigestsRequest xmlns="http://www.ebics.org/H003" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<?xml version="1.0" encoding="UTF-8"?>
<ebicsNoPubKeyDigestsRequest xmlns="http://www.ebics.org/H003" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.ebics.org/H003 http://www.ebics.org/H003/ebics_keymgmt_request.xsd" Version="H003" Revision="1">
<header authenticate="true">
<static>
<HostID>EBIXQUAL</HostID>
<Nonce>234AB2340FD2C23035764578FF3091C1</Nonce>
<Timestamp>2015-11-13T10:32:30.123Z</Timestamp>
<PartnerID>AD598</PartnerID>
<UserID>EF056</UserID>
<OrderDetails>
<OrderType>HPB</OrderType>
<OrderAttribute>DZHNN</OrderAttribute>
</OrderDetails>
<SecurityMedium>0000</SecurityMedium>
</static>
<mutable />
</header>
</ebicsNoPubKeyDigestsRequest>
但当我尝试签名时,我在网上得到了这个错误
signedXml.ComputeSignature()
:
不正确的参考元素
你能帮我解决我的问题吗
提前谢谢你
托马斯 我通过SignedXml和引用类以及。。我可以在单独的答案中向您提供所有详细信息,但我现在的结论是,您只能有两种类型的查询:
#xpointer(/)
这是因为它是显式检查的,并且
#xpointer(id(
再次显式(使用string.StartsWith)选中此选项
因此,正如您在评论中指出的那样,实现这一点的唯一方法似乎是扩展SignedXml类并覆盖GetIdeElement方法,如下所示:
public class CustomSignedXml : SignedXml
{
XmlDocument xmlDocumentToSign;
public CustomSignedXml(XmlDocument xmlDocument) : base(xmlDocument)
{
xmlDocumentToSign = xmlDocument;
}
public override XmlElement GetIdElement(XmlDocument document, string idValue)
{
XmlElement matchingElement = null;
try
{
matchingElement = base.GetIdElement(document, idValue);
}
catch (Exception idElementException)
{
Trace.TraceError(idElementException.ToString());
}
if (matchingElement == null)
{
// at this point, idValue = xpointer(//*[@authenticate='true'])
string customXPath = idValue.TrimEnd(')');
customXPath = customXPath.Substring(customXPath.IndexOf('(') + 1);
matchingElement = xmlDocumentToSign.SelectSingleNode(customXPath) as XmlElement;
}
return matchingElement;
}
}
然后在消费者代码中,只需将SignedXml更改为CustomSignedXml:
CustomSignedXml signedXml = new CustomSignedXml(xmlDoc);
Ebics协议不接受header元素上的id属性,因此我不能使用您的解决方案。对此很抱歉,因为我从我的研究中了解到,我主要说明了问题和唯一的解决方法。您认为我可以覆盖
getidement
,使用#xpointer(/*[@authenticate='true')]
来选择好的节点吗?但是我不知道我在哪里可以做是的。SignedXml类不是密封的,方法是虚拟的。扩展SignedXml类并重写此方法,然后您可以在try-catch块中测试基方法,如果它失败或返回null,您可以找到xml元素并返回itI,如果您有一个小时的时间,可以将其添加到我的答案中
CustomSignedXml signedXml = new CustomSignedXml(xmlDoc);