[英]“malformed reference element” exception signing a XML file
我正在嘗試使用對 KeyInfo 節點的引用對 XML 文檔進行簽名,但在調用方法“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 標記的引用。
我終於找到了答案,我無法添加 KeyInfoId 引用,因為 XMLDoc 中尚未創建 xmlElement; 所以我實現了 SigneXML 類並直接從 keyInfo 返回 XML。
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);
}
我希望它有幫助!!
就我而言,在被困幾天之后。 它是 Reference.uri。 我將它保存為“#1”。 XML是。
<getTest>
<item ID="1">
<Seed>123</Seed>
</item>
</getTest>
在 Windows Server 2003 上運行正常,但在 Windows 10 和 Windows Server 2012 R2 服務器上的開發人員機器上失敗。 它落入ComputeSignature方法。
最后我讀到 ID 不能是數字,如果它必須以字母開頭。 https://www.w3.org/TR/html401/types.html#type-name我將其更改為 test reference.uri = "#test" 和我更改的基本 XML 如何顯示如下。
<getTest>
<item ID="test">
<Seed>123</Seed>
</item>
</getTest>
瞧,它適用於所有環境。
SO 新補丁中有一些補丁會導致它失敗,並且在沒有這些補丁的舊補丁中不起作用,而這些補丁是設置限制的補丁。
KB3140745
KB3140768
KB3140743
KB3073930
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.