Help convert C# code relating to signing xml

 Hi

To summarize:

In VC 2.3 for VS, we need to digitally sign portions of an XML document in SOAP envelope.  We are using classes in System.Security.Cryptography.Xml, in particular the class SignedXml

We are having trouble signing xml when we add a Reference based on an Id attribute.  We are getting "Malformed Reference Element" exception when ComputeSignature is invoked.

After much searhing on the internet I made have found the answer in stackoverflow, but I need to convert C# code below:

 

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 to see if it's a standard ID reference
XmlElement idElem = base.GetIdElement(doc, id);

if (idElem == null)
{
XmlNamespaceManager nsManager = new XmlNamespaceManager(doc.NameTable);
nsManager.AddNamespace("wsu", "docs.oasis-open.org/.../oasis-200401-wss-wssecurity-utility-1.0.xsd");

idElem = doc.SelectSingleNode("//*[@wsu:Id=\"" id "\"]", nsManager) as XmlElement;
}

return idElem;
}
}

 

Converting the above code may be the answer, otherwise I may need more help.

Many thanks

Brendan

Parents
  • Verified Answer

    Something like this:

     

          $set ilusing"System.Security.Cryptography.Xml"
          $set ilusing"System.Xml"
           class-id testsignxml.SignedXmlWithId inherits type SignedXml.
    
           working-storage section.
           method-id New public.
           procedure division using myxml as type XmlDocument.
           
              invoke super::New(myxml)
              goback.
           end method.
           method-id New public.
           procedure division using xmlElement as type XmlElement.
           
              invoke super::New(xmlElement)
              goback.
           end method.
    
           method-id GetIdElement override.
           procedure division using by value doc as type XmlDocument #id as string
               returning return-item as type XmlElement.
               declare idElem as type XmlElement = super::GetIdElement(doc, #id)
               if idElem = null
                  declare nsManager as type XmlNamespaceManager = new XmlNamespaceManager(doc::NameTable)
                  invoke nsManager::AddNamespace("wsu", "docs.oasis-open.org/.../oasis-200401-wss-wssecurity-utility-1.0.xsd")
                  set idElem = doc::SelectSingleNode("//*[@wsu:Id=\" & #id & "\]", nsManager) as type XmlElement
               end-if
               set return-item to idElem
               goback
           end method.
          
           end class.
    
    
Reply
  • Verified Answer

    Something like this:

     

          $set ilusing"System.Security.Cryptography.Xml"
          $set ilusing"System.Xml"
           class-id testsignxml.SignedXmlWithId inherits type SignedXml.
    
           working-storage section.
           method-id New public.
           procedure division using myxml as type XmlDocument.
           
              invoke super::New(myxml)
              goback.
           end method.
           method-id New public.
           procedure division using xmlElement as type XmlElement.
           
              invoke super::New(xmlElement)
              goback.
           end method.
    
           method-id GetIdElement override.
           procedure division using by value doc as type XmlDocument #id as string
               returning return-item as type XmlElement.
               declare idElem as type XmlElement = super::GetIdElement(doc, #id)
               if idElem = null
                  declare nsManager as type XmlNamespaceManager = new XmlNamespaceManager(doc::NameTable)
                  invoke nsManager::AddNamespace("wsu", "docs.oasis-open.org/.../oasis-200401-wss-wssecurity-utility-1.0.xsd")
                  set idElem = doc::SelectSingleNode("//*[@wsu:Id=\" & #id & "\]", nsManager) as type XmlElement
               end-if
               set return-item to idElem
               goback
           end method.
          
           end class.
    
    
Children
No Data