artsince
artsince

Reputation: 1032

How To User XmlDsigEnvelopedSignatureTransform independent of SignedXml class

All the examples on the internet shows the usage of the XmlDsigEnvelopedSignatureTransform class as a part of the SignedXml class.

I would like to use this class by itself. Just give it a nodeList get the result without the signature node.

XmlDsigEnvelopedSignatureTransform envSigTrans = new XmlDsigEnvelopedSignatureTransform();
envSigTrans.LoadInput(xmlDocument.DocumentElement.SelectNodes("//*"));
XmlNodeList nodeList = (XmlNodeList)envSigTrans.GetOutput(typeof(XmlNodeList));

But the output nodeList still contains the ds:Signature node I would like to get rid of.

What is the correct way of doing this? Your help is very appreciated.

Upvotes: 0

Views: 1641

Answers (2)

Yaron Naveh
Yaron Naveh

Reputation: 24406

Just came into the same issue myself. Turns out that if you use XmlDsigEnvelopedSignatureTransform "as is" you will always get all nodes back. I have tracked down XmlDsigEnvelopedSignatureTransform in the reflector and it will only remove the signature if it has a value in _signaturePosition. This is a private API though which only MS uses intertnally (by SignedInfo) so by large XmlDsigEnvelopedSignatureTransform is not usefull as a user. The good news is that you can set it manually via private reflection. If your Xml has only one signature element in it the value should always be one.

    var transform = new XmlDsigEnvelopedSignatureTransform(false);
    transform.LoadInput(xmldoc);        
    FieldInfo field = transform.GetType().GetField("_signaturePosition", 
                     BindingFlags.NonPublic |
                     BindingFlags.Instance);
    field.SetValue(transform, 1);                
    var res = (XmlDocument)t.GetOutput();
    var str = res.OuterXml;

Upvotes: 4

gnumaru
gnumaru

Reputation: 311

"But the output nodeList still contains the ds:Signature node I would like to get rid of."

If you just want to get rid of the element, why don't you just remove it from the list?

Have a look at this:

How to remove an XmlNode from XmlNodeList

Upvotes: 0

Related Questions