Adding a custom SOAPHeader in C # to call a web service

I am trying to add custom soap header information in C # before calling the web service. I am using the SOAP header class to do this. I could do it partially, but not completely, as I need it. This is how I need a soap bar to look like

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <soap:Header> <Security xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"> <UsernameToken> <Username>USERID</Username> <Password>PASSWORD</Password> </UsernameToken> </Security> </soap:Header> <soap:Body> ... 

I can add a soap bar below

 <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <soap:Header> <UsernameToken xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"> <Username>UserID</Username> <Password>Test</Password> </UsernameToken> </soap:Header> <soap:Body> 

What I cannot do is add security elements that wrap the "UsernameToken", as in the first example. Any help would be appreciated.

+8
c # soapheader
source share
1 answer

This link adding a soap header worked for me. I am calling the SOAP 1.1 service, which I have not written or controlled. I am using VS 2012 and added the service as a web link in my project. Hope this helps

I followed steps 1-5 from the post of J. Dagen at the bottom of the stream.

Here is a sample code (this will be in a separate .cs file):

 namespace SAME_NAMESPACE_AS_PROXY_CLASS { // This is needed since the web service must have the username and pwd passed in a custom SOAP header, apparently public partial class MyService : System.Web.Services.Protocols.SoapHttpClientProtocol { public Creds credHeader; // will hold the creds that are passed in the SOAP Header } [XmlRoot(Namespace = "http://cnn.com/xy")] // your service namespace goes in quotes public class Creds : SoapHeader { public string Username; public string Password; } } 

And then in the generated proxy class, in the method that calls the service, in step 4 of J Dudgeon add this attribute: [SoapHeader("credHeader", Direction = SoapHeaderDirection.In)]

finally, here's the call to the generated proxy method with the header:

 using (MyService client = new MyService()) { client.credHeader = new Creds(); client.credHeader.Username = "username"; client.credHeader.Password = "pwd"; rResponse = client.MyProxyMethodHere(); } 
+1
source share

All Articles