本文介绍了“格式错误的参考元素"指的是“格式错误的参考元素".签名XML文件的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用对KeyInfo节点的引用对XML文档进行签名,但是在调用方法"ComputeSignature"之后却遇到了格式错误的引用元素"异常.

I'm trying to sign a XML document with a reference to the KeyInfo node but I'm getting "malformed reference element" exception after calling the method "ComputeSignature".

这是我的代码:

        signedXml.SigningKey = certificate.PrivateKey;

        if (!signParameters.IncludeCertificateInSignature) return;
        var certificateKeyInfo = new KeyInfo();
        certificateKeyInfo.AddClause(new KeyInfoX509Data(certificate));
        signedXml.KeyInfo = certificateKeyInfo;
        signedXml.KeyInfo.Id = "xmldsig-keyinfo";

        signedXml.AddReference(new Reference("#xmldsig-keyinfo"));

如果删除#xmldsig-keyinfo",它可以工作,但是我得到了整个文档的引用,我需要带有KeyInfo标签的引用.

If I remove the "#xmldsig-keyinfo", it works, but I get the reference with the entire document, I need the reference with the KeyInfo tag.

推荐答案

我终于找到了答案,我无法添加KeyInfoId引用,因为尚未在XMLDoc中创建xmlElement.因此我实现了SigneXML类,并直接从keyInfo返回XML.

I finally found the answer, I can't add the KeyInfoId reference because the xmlElement is not created yet in the XMLDoc; so I implemented the SigneXML class and return the XML directly from the keyInfo.

public override XmlElement GetIdElement(XmlDocument doc, string id)
{
    if (String.Compare(id, this.KeyInfo.Id, StringComparison.OrdinalIgnoreCase) == 0)
        return this.KeyInfo.GetXml();
    else
        return base.GetIdElement(doc, id);
}

我希望对您有帮助!

这篇关于“格式错误的参考元素"指的是“格式错误的参考元素".签名XML文件的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-27 21:53