How to Add Authorization with Bearer Token to Soap Request -> Add Soap Action
Closed this issue ยท 12 comments
I want to use your SoapHttpClient for making Soap Request but I have difficulties in setting it up. Biggest hurdle is adding a Bearer Token to the SOAP Request.
In the SOAP UI I can add a Bearer token like this:
How can I do the same thing trough code? (@RichardSlater @pmorelli92 @pmorelli-origin)
What I tried in steps:
- Generating the Token trough the TokenClient of IdentityModel package.
- Add it as a header like this:
var header = new XElement("Authorization", $"Bearer {token}");
- Sending the request.
using (var soapClient = new SoapClient())
{
var result =
await soapClient.PostAsync(
endpoint: endpoint,
soapVersion: SoapVersion.Soap11,
body: body,
header: header);
Log.Information(await result.Content.ReadAsStringAsync());
}
- Inspecting the result
Internal Server Error
I think this is most probably because I didn't add the token in the right way.
I tried to remove the Token Authorization now from my Service and after removing the header from my call I still get the Internal Server Error
. The call works perfectly in the SoapUI.
What can I do to better debug the error?
Hi @maracuja-juice, have you tried setting the header in the delegate of the HttpClient?
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Authorization = ....
var soapClient = new SoapClient(() => httpClient)
Hi @pmorelli92
Thanks for answering. No I haven't.
Currently I have no Authorization in my SOAP Service and I also get the Internal Server Error
.
What can I do to better debug this error as it isn't only the token?
@maracuja-juice I would suggest installing Fiddler in order to see how is the call being made to the service.
@pmorelli92
Okay. What I found out trough fiddler:
- The XML body is not the issue (the XML that my app sends works fine when sending the same XML with the Soap UI)
- The request headers seem to be the issue.
These are the request header that get sent from my app trough the SoapHttpClient:
And these are the request headers that get sent from the Soap UI:
The difference are:
- SoapUI uses Content-Length: 236 instead of 240
- SoapUI sends a User Agent
- SoapUI sends a Soap Action
I suspect that it's the SoapAction header that makes the difference.
How can I add this header to my request when using the PostAsync method?
@maracuja-juice are you sending the action parameter in the post async call?
@pmorelli92 Good point.
Do you mean like this?
var result = await soapClient.PostAsync(
endpoint: endpoint,
soapVersion: SoapVersion.Soap11,
body: body,
action: "IsAlive"); // Or with the full URL.
That doesn't add a SoapAction to the Request Headers but an ActionHeader.
Then you can do something like:
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("SOAPAction", "foobar");
var soapClient = new SoapClient(() => httpClient)
@pmorelli92
Oh cool that works if I put the full URL like in the request of the SOAP Ui there.
I don't have much experience with SOAP but isn't that SOAPAction header something that should be added per default?
I am going to expose an overload for adding the action in the client.PostAsync
Nice! ๐ When will you do that?
@maracuja-juice sorry for the delay, this is because of #8. It should be working on the just released version 2.2 using the action parameter of the post signature.