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
  • 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.

Reply
  • 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.

Children
No Data