How to add a security header to a SOAP message?

I am trying to use a WebService written in Java by our provider from our C # application. When it's time to chat, I get the following:

WSDoAllReceiver: Incoming message does not contain the required security header.

Since yesterday, I am trying to figure out how to add a security header to a SOAP message.

Yes, I read it ( I don’t know how to create a SOAP <wsse: Security> header ), but it didn’t work.

I looked around, and this seems to be a pretty asked question. I wonder if I can get some help here, some pointers, some code to get me started.

+7
source share
2 answers

I really managed to achieve this using WSE. The funny thing is that the provider’s web services will not work with WSE 3.0, but they did it with WSE 2.0. Here are the steps

  • Get WSE 2.0
  • Add web link to the project
  • In the implementation of the Web Reference proxy server:

Replace

public partial class UserWS : System.Web.Services.Protocols.SoapHttpClientProtocol 

 public partial class UserWS : Microsoft.Web.Services2.WebServicesClientProtocol 
  • Before calling the web service:

Set authentication information

 UsernameToken token = new UsernameToken("user", "pwd", PasswordOption.SendPlainText); yourProxy.RequestSoapContext.Security.Tokens.Add(token); 

And this! FYI, the provider is a Blackboard instance.

+14
source

Try this. There is no need for a web link and implementation of Web.Services2.

 var client = "Your Service Client"; using (var scope = new OperationContextScope(client.InnerChannel)) { System.Xml.XmlDocument document = new XmlDocument(); XmlElement element = document.CreateElement("wsse", "UsernameToken", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"); XmlElement newChild = null; newChild = document.CreateElement("wsse", "Username", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"); newChild.InnerText = "finance"; element.AppendChild(newChild); newChild = document.CreateElement("wsse", "CorporationCode", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"); newChild.InnerText = "387"; element.AppendChild(newChild); MessageHeader messageHeader = MessageHeader.CreateHeader("UsernameToken", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", element, false); // shouldn't MessageHeader be Security? // MessageHeader messageHeader = MessageHeader.CreateHeader("Security", ... OperationContext.Current.OutgoingMessageHeaders.Add(messageHeader); var result = client.GetCorporations(new CorporationType { pageNo = 1 }); } 
+2
source

Source: https://habr.com/ru/post/886970/


All Articles