gpt4 book ai didi

c# - 添加基于带有 SignedXml 类的 Id 属性的引用时出现“格式错误的引用元素”

转载 作者:IT王子 更新时间:2023-10-29 04:41:30 26 4
gpt4 key购买 nike

当存在命名空间前缀时,无法通过 Id 属性对元素进行签名:

void Main()
{
var doc = new XmlDocument();
doc.LoadXml("<root xmlns:u=\"myuri\"><test u:Id=\"_0\">Zebra</test></root>");

SignedXml signedXml = new SignedXml(doc);
signedXml.SigningKey = new RSACryptoServiceProvider();

Reference reference = new Reference("#_0");
signedXml.AddReference(reference);

signedXml.ComputeSignature();
}

ComputeSignature() 将在这里失败并显示“格式错误的引用元素”,应该如何完成?

最佳答案

我们使用的方法是继承 System.Security.Cryptography.Xml.SignedXml 类...

public class SignedXmlWithId : SignedXml
{
public SignedXmlWithId(XmlDocument xml) : base(xml)
{
}

public SignedXmlWithId(XmlElement xmlElement)
: base(xmlElement)
{
}

public override XmlElement GetIdElement(XmlDocument doc, string id)
{
// check to see if it's a standard ID reference
XmlElement idElem = base.GetIdElement(doc, id);

if (idElem == null)
{
XmlNamespaceManager nsManager = new XmlNamespaceManager(doc.NameTable);
nsManager.AddNamespace("wsu", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");

idElem = doc.SelectSingleNode("//*[@wsu:Id=\"" + id + "\"]", nsManager) as XmlElement;
}

return idElem;
}
}

关于c# - 添加基于带有 SignedXml 类的 Id 属性的引用时出现“格式错误的引用元素”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5099156/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com