Created
November 1, 2022 12:04
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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