“参考元素格式错误”使用 SignedXml 类添加基于 Id 属性的引用时
当存在命名空间前缀时,无法通过 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()
将在此处失败,并显示“格式错误的引用元素”,该怎么办?
Unable to sign element by Id attribute when there's a namespace prefix:
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()
will fail here with 'Malformed Reference Element' how should this be done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
var 引用 = 新引用(""); // 这将签署整个文档
var reference = new Reference(""); // This will sign the entire document
应该注意的是,您需要使用
SignedXmlWithId
对象而不是SignedXml
对象,以便能够使用重写的GetIdElement()
方法。完成此操作后,我就能够签署XmlElement
并解决格式错误的引用元素错误。在此处查看我关于此主题的帖子。
It should be noted that you will need to use
SignedXmlWithId
object instead ofSignedXml
object in order to be able to use the overriddenGetIdElement()
method. Once I did that, I was able to sign anXmlElement
and get around the Malformed Reference Element error.See my post about this topic here.
SignedXml 无法将 u:Id 识别为有效的 XML ID,并且 XML 签名确实要求它是 XML ID。
您可以使用架构(http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd,如果您尝试使用 WS-Security Id)或将 DTD 添加到 XML 片段。 ( ]> 对于 XML 片段)。仅将 DTD 添加到 LoadXml 将使 SignedXml 识别 Id,但由于 SOAP 不允许 DTD,因此请勿在在线 SOAP 中包含 DTD。
SignedXml does not recognize u:Id as a valid XML ID, and the XML Signature does require it to be an XML ID.
You can either use the Schema (http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd if your trying to use a WS-Security Id) or add an DTD to the XML fragment. ( ]> for an XML fragment). Adding a DTD to just your LoadXml will make SignedXml recognize the Id, but since SOAP does not allow DTD's, don't include the DTD in your on-the-wire SOAP.
我们使用的方法是子类化
System.Security.Cryptography.Xml.SignedXml
类...The approach we used was to subclass
System.Security.Cryptography.Xml.SignedXml
class...