Having problems with your account or logging in?
A lot of changes are happening in the community right now. Some may affect you. READ MORE HERE
Highlighted
brendang
New Member.
406 views

Help convert C# code relating to signing xml

Jump to solution

 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

0 Likes
1 Solution

Accepted Solutions
Micro Focus Expert
Micro Focus Expert

RE: Help convert C# code relating to signing xml

Jump to solution

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.

0 Likes
4 Replies
Micro Focus Expert
Micro Focus Expert

RE: Help convert C# code relating to signing xml

Jump to solution

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.

0 Likes
brendang
New Member.

RE: Help convert C# code relating to signing xml

Jump to solution

Hi Chris

Many thanks, using that code may well have done the trick regarding the signature.

Perhaps I should create a new post, but now we have the problem of sending the SOAP message, which we use System.Xml, System.Security.Cryptography..., etc to create.

What I have tried so far is the below:

method-id SEND-SOAP.

01 Client type HttpClient.

01 Request type HttpRequestMessage.

01 Response type HttpResponseMessage.

*01 Uri String.
01 Uri type Uri.

procedure division.

set Uri to new Uri("https://???.ie/paye-employers/v1/soap").  *>> actual URI replaced here

set Client to new HttpClient().

set Request to new HttpRequestMessage(type HttpMethod::Post, Uri).

set Request::Content to new StringContent(theMsg, type Encoding::UTF8, "application/soap+xml").  *>> theMsg is the SOAP envelope

* set Response to Client::SendAsync(Request) as type HttpResponseMessage.
set Response to Client::SendAsync(Request)::Result.

* set Response to Client::PostAsync(Uri,Request::Content)::Result.

exit method.

end method.

 

This is returning Http status code 500 "Internal Server Error", which is not very helpful. I may try things like sending the data as a stream.

Using System.Net.Http is probably not the best way to go about it.  I have looked at some WCF examples but I do not understand them.

We have been provided with the .wsdl's, endpoints, and sample code written in Java.

Any help would be appreciated.

 

Many thanks

Brendan.

0 Likes
Micro Focus Expert
Micro Focus Expert

RE: Help convert C# code relating to signing xml

Jump to solution
Hi Brendan,

Please create a new thread for the new problem so that we can close out the other one. It really makes it more difficult to search for particular problems and solutions in the future if their are multiple problems are questions asked within a single thread.

Thanks.
0 Likes
brendang
New Member.

RE: Help convert C# code relating to signing xml

Jump to solution

Hi Chris

Ok thanks, thought I should probably do that. [:)]  New post "How to send digitally-signed SOAP message" created.

 

Thanks

Brendan

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.