Adding custom SOAPHeader in C# for a web service call - c#

I am trying to add a custom soap header information in c# before calling a web service. I am using SOAP Header class to get this done. I could do this partly but not completely the way I need it. Here is how I need the soap header 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 am able to add soap header as 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 am not able to do is add the "Security" elements which wraps the "UsernameToken" as in the first sample. Any help would be appreciated.

This link adding soap header worked for me. I am calling a SOAP 1.1 service that I did not write and have no control over. Am using VS 2012 and added the service as a web reference in my project. Hope this helps
I followed the steps 1-5 from J. Dudgeon's post towards the bottom of the thread.
Here's some sample code (this would 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's namespace goes in quotes
public class Creds : SoapHeader
{
public string Username;
public string Password;
}
}
And then in the generated proxy class, on the method that calls the service, per J Dudgeon's step 4 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();
}

Related

Implement SOAP 1.2 service in asp.net core

I'm trying to implement OCPP 1.5 under ASP.Net Core 2.2. The standard makes use of SOAP 1.2. The issue comes from poor interpretation of the MessageHeader attribute. Properties with MessageHeader should be in header, but they are not.
Source code: https://github.com/delianenchev/OcppDemo
I use SoapCore under ASP.Net Core. My initialization looks like this:
var transportBinding = new HttpTransportBindingElement();
var textEncodingBinding = new TextMessageEncodingBindingElement(MessageVersion.Soap12WSAddressingAugust2004, System.Text.Encoding.UTF8);
var customBinding = new CustomBinding(transportBinding, textEncodingBinding);
app.UseSoapEndpoint<ISampleService>("/SOAP/service.wsdl", customBinding, SoapSerializer.DataContractSerializer);
My demo model with the MessageHeader and MessageBodyMember attributes.
[MessageContract]
public class MessageModelRequest
{
[MessageHeader]
public string Id { get; set; }
[MessageBodyMember]
public string Name { get; set; }
[MessageBodyMember]
public string Email { get; set; }
}
I test API with SoapUI.
This is my API under ASP.NET core with SoapCore.
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:tem="http://tempuri.org/" xmlns:mod="http://schemas.datacontract.org/2004/07/Models">
<soap:Header/>
<soap:Body>
<tem:TestMessageModel>
<!--Optional:-->
<tem:inputModel>
<!--Optional:-->
<mod:Email>?</mod:Email>
<!--Optional:-->
<mod:Id>1</mod:Id>
<!--Optional:-->
<mod:Name>?</mod:Name>
</tem:inputModel>
</tem:TestMessageModel>
</soap:Body>
</soap:Envelope>
Correct API from WCF project for IIS.
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
<soapenv:Header>
<tem:Id>34</tem:Id>
</soapenv:Header>
<soapenv:Body>
<tem:MessageModelRequest>
<!--Optional:-->
<tem:Email>3</tem:Email>
<!--Optional:-->
<tem:Name>4</tem:Name>
</tem:MessageModelRequest>
</soapenv:Body>
</soapenv:Envelope>
Well, judging by the source code of SoapCore it seems to support message headers for reading the SOAP message as it uses MessageEncoder for that purpose which knows exactly how to read a SOAP message, but when it comes to serializing a response in your case it uses a native DataContractSerializer for writing the body that ignores any message contract attributes you have on your class and furthermore it doesn't have any part for writing header, just the message body.
So I guess you need to implement the header support in response messages by yourself.
First of all, add IgnoreMemberAttribute (or XmlIgnoreAttribute if you switch to SoapSerializer.XmlSerializer) on the properties you intend to add to your response message header so that data contract serializer doesn't add them to the body of the message.
Finally, you will need to locate the properties decorated with MessageHeader attribute manually and add them to your header. Luckily SoapCore has multiple options for doing that as suggested here.
As alternative if you plan to include the source of SoapCore in your solution, you could easily achieve the goal somewhere along these lines. It's easy to do so because at this place you have the full control of the message and the response you got from your service method. With the aid of reflection, you can easily find the properties of responseObject which need to be moved to the header and just forward them to responseMessage.Headers.
I know it's a bit nasty, but well... this is the price of using SOAP in .NET Core.
In .NET 6, you can, for example, do the following to switch to SOAP 1.2 (a.k.a. soap12):
// In Program.cs ...
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
// ...
app.UseSoapEndpoint<IMyServiceContract>("/MyService.asmx", new SoapEncoderOptions()
{
// Use SOAP version 1.2 (aka Soap12)
MessageVersion = MessageVersion.Soap12WSAddressingAugust2004
// - OR -
MessageVersion = MessageVersion.Soap12WSAddressing10
}, caseInsensitivePath: true);
That will result in:
<wsdl:definitions xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" ...>

How Do I Change XML Prefix For WCF Client Request c#?

Due to requirements from a web service that I have no control over, I need to change my ns prefixes from the defaults of "s" and "h". The service provider has provided a .wsdl file for my service reference, however, they do not appear to have an online wsdl for metadata exchange. I have emailed them and they confirmed that the prefixes have to be specific values (right or wrong on their part, I have to make this work).
Can someone please tell me the easiest way to change these values? Is there something I can modify in Reference.cs? Do I have to implement some sort of message formatting logic into my requests to change the values just prior to sending out the request?
Every element in the XML has to have a prefix of a specific value (2 values total), or the WS will not accept the request and simply returns HTML in the response. When I copy the request captured in Fiddler, over to SOAP UI and update the prefixes to what they require, the request works fine when executed from Soap UI. I'm simply not finding any simple solutions to this seemingly simple problem for c# VS 2017 development platform.
Also worth noting, when I load the .wsdl file into Soap UI, Soap UI generates all of the ws operations and requests with the correct, desired prefixes that I need in my c# app. How does Soap UI know the exact prefixes to generate in the requests?
I need to modify this:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<h:FooListHeader xmlns:h="http://foo.foo.com/Hello" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<h:FooList>
<h:FooStatement>
<h:Title>Foo</h:Title>
<h:Value>123</h:Value>
</h:FooStatement>
</h:FooList>
</h:FooListHeader>
</s:Header>
<s:Body>
<GetFooRequestType xmlns="http://foo.foo.com/Hello">
<MessageRequest xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<ConFooRequest/>
</MessageRequest>
</GetFooRequestType>
</s:Body>
</s:Envelope>
To something like this:
<soapenv:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:foo="http://foo.foo.com/Hello">
<soapenv:Header>
<foo:FooListHeader xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<foo:FooList>
<foo:FooStatement>
<foo:Title>Foo</foo:Title>
<foo:Value>123</foo:Value>
</foo:FooStatement>
</foo:FooList>
</foo:FooListHeader>
</soapenv:Header>
<soapenv:Body>
<foo:GetFooRequestType xmlns="http://foo.foo.com/Hello">
<foo:MessageRequest xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<foo:ConFooRequest/>
</foo:MessageRequest>
</foo:GetFooRequestType>
</soapenv:Body>
</soapenv:Envelope>
Example c# code:
public GetYaddaYaddaResponseType CheckConnection()
{
Yadda client = new Yadda();
var msgRequest = new GetYaddaYaddaResponseType { ConnectionConfirmationRequest = new ConfirmConnectRequestType() };
List<Statement> statementList = new List<Statement>
{
new Statement { Name = "Yo", Value = "123" },
new Statement { Name = "Hey", Value = "123" },
};
var header = new StatementHeader
{
StatementList = statementList.ToArray()
};
var response = client.GetYaddaYaddaMessage(ref header, msgRequest);
return response;
}

How to read soap response in c#?

Soap Respose as:
<?xml version="1.0" encoding="utf-8" ?>
<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:Body>
<LoginResponse xmlns="http://example.com/SystemIntegration">
<FirstName>#FirstName</FirstName>
<LastName>#LastName</LastName>
</LoginResponse>
</soap:Body>
</soap:Envelope>
I'm trying to read it as:
XDocument doc = XDocument.Parse(strReturnStatus);
List<XElement> result = doc.Elements("LoginResponse").ToList();
for (int intc = 0; intc <= result.Count - 1; intc++)
{
strResponseCode = result[intc].Element("FirstName").Value.ToString();
strResponseText = result[intc].Element("LastName").Value.ToString();
}
But it returning null result.
How to read above respose in asp.net c#??
The easiest thing is to create a proxy class for your service.
You can do that using the 'Add Service Reference' option in Visual Studio. Enter the URL of the service, and Visual Studio will generate the source code for you.
From that point you can access the service using C# code. No need to manually extract the payload of the SOAP message.
Note if you are the implementator of the service: ASMX webservices are deprecated for a long time already. If you can, use WCF.
Use Descendants() method in XDocumentobject to navigate through XML nodes to get elements, you can follow steps provided in this post Using C# to parse a SOAP Response

SOAP API with DomainBox in ASP.NET application

I am trying to integrate an ASP.NET application to http://www.domainbox.com/ using their SOAP API. I couldn't find any examples. The only thing they offer is the SOAP Resquest and Response examples. My question is how do I call the request and how to I get the response data to use in my application?
REQUEST:
<soap12:Envelope xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xmlns:xsd=”http://www.w3.org/2001/XMLSchema” xmlns:soap12=”http://www.w3.org/2003/05/
soap-envelope”>
<soap12:Body>
<QueryDomainRenewalSettings xmlns=”https://sandbox.domainbox.net/”>
<AuthenticationParameters>
<Reseller>myreseller</Reseller>
<Username>myusername</Username>
<Passwordmy>password</Password>
</AuthenticationParameters>
<CommandParameters>
<DomainName>atestdomain.co</DomainName>
</CommandParameters>
</QueryDomainRenewalSettings>
</soap12:Body>
</soap12:Envelope>
RESPONSE:
<soap:Envelope xmlns:soap=”http://www.w3.org/2003/05/soap-envelope”
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xmlns:xsd=”http://www.
w3.org/2001/XMLSchema”>
<soap:Body>
<QueryDomainRenewalSettingsResponse xmlns=”https://sandbox.domainbox.net/”>
<QueryDomainRenewalSettingsResult>
<ResultCode>100</ResultCode>
<ResultMsg>Domain Renewal Settings Queried Successfully</ResultMsg>
<TxID>4d76201a-3b6d-4ccc-bca5-729439bbac9b</TxID>
<DomainId>87967</DomainId>
<AutoRenew>true</AutoRenew>
<AutoRenewDays>60</AutoRenewDays>
</QueryDomainRenewalSettingsResult>
</QueryDomainRenewalSettingsResponse>
</soap:Body>
</soap:Envelope>
Thanks.
You should add the Domainbox API as a service reference if you're using ASP.NET.
That will let you use it just like a normal C#/VB class,
var parameters = new Domainbox.CheckDomainAvailabilityParameters {DomainName= "example.com"};
var result = apiObject.CheckDomainAvailiability(authObject, paramters);
if (result.ResultCode == 100) { // get results... }
I can provide more detailed example if required.

Consuming in Java a .NET Web Service that requires a custom SOAP Header

So I need to consume a Web Service that uses a custom SoapHeader, as described below. What is the simplest way to pass the correct values through this header using Java. I'm using Netbeans.
<?xml version="1.0" encoding="utf-8"?\>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header>
<CustomSoapHeader xmlns="http://snip">
<UserName>"string"</UserName>
<Password>"string"</Password>
</CustomSoapHeader>
</soap:Header>
<soap:Body>
<SomeWebMethod xmlns="http://snip" />
</soap:Body>
</soap:Envelope>
EDIT: What's the best way to display XML on Stack Overflow?
It might help to add that the Web Service is implemented in .NET and I cannot change the server side code.
Here are the basic steps, assuming you're doing this on the client side:
Install a HandlerResolver on your service interface (service.setHandlerResolver())
Override HandlerResolver.getHandlerChain() to insert your own implementation of SOAPHandler
Implement SOAPHandler.handleMessage() to modify the SOAP header before it's sent out
You can pass parameters to your handler through the request context:
Map<String, Object> context = ((BindingProvider) port).getRequestContext();
context.put("userName', "foo");
...
in handleMessage() you can get at the header like this:
public boolean handleMessage(SOAPMessageContext context) {
...
SOAPMessage msg = context.getMessage();
msg.getSoapHeader();
...
}
Hope that helps. I'm guessing there's also a way to do this stuff with annotations as well.

Categories