Skip to content

Instantly share code, notes, and snippets.

@nikanos
Created November 1, 2022 12:04
Show Gist options
  • Save nikanos/38df54c4bd61ece6f1a04a53a424e154 to your computer and use it in GitHub Desktop.
Save nikanos/38df54c4bd61ece6f1a04a53a424e154 to your computer and use it in GitHub Desktop.
SignedXmlWithReferenceIdAttributeName - class inherited from SignedXml that gives the option to specify the name of the id attribute when signing XML
using System;
using System.Security.Cryptography;
using System.Security.Cryptography.Xml;
using System.Xml;
namespace NS
{
class SignedXmlWithReferenceIdAttributeName : SignedXml
{
private string _referenceIdAttributeName;
public SignedXmlWithReferenceIdAttributeName(XmlDocument xmlDocument, string referenceIdAttributeName)
: base(xmlDocument)
{
if (referenceIdAttributeName == null)
throw new ArgumentNullException(nameof(referenceIdAttributeName));
_referenceIdAttributeName = referenceIdAttributeName;
}
public SignedXmlWithReferenceIdAttributeName(XmlElement xmlElement, string referenceIdAttributeName)
: base(xmlElement)
{
if (referenceIdAttributeName == null)
throw new ArgumentNullException(nameof(referenceIdAttributeName));
_referenceIdAttributeName = referenceIdAttributeName;
}
public override XmlElement GetIdElement(XmlDocument document, string idValue)
{
XmlNodeList xmlNodeList = document.SelectNodes(string.Format("//*[@{0}='{1}']", _referenceIdAttributeName, idValue));
if (xmlNodeList == null || xmlNodeList.Count == 0)
return null;
if (xmlNodeList.Count == 1)
return xmlNodeList[0] as XmlElement;
else
throw new CryptographicException($"Should not exist more than one elements with attribute {_referenceIdAttributeName} and value {idValue}.");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment