c# - 'Malformed Reference Element' when adding a reference based on an Id attribute with SignedXml class -
unable sign element id attribute when there's 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()
fail here 'malformed reference element' how should done?
the approach used subclass system.security.cryptography.xml.signedxml
class...
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 see if it's 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) xmlelement; } return idelem; } }
Comments
Post a Comment