1 year ago
#355340
Always Learning
SOAP call from .net core with Basic Authentication
I am trying to migrate some very old asp.net code to .net core, and there are many calls to a SOAP service.
I can add the connected service fine, but when I try and call it I always get the following response
***The HTTP request is unauthorized with client authentication scheme 'Anonymous'.
The authentication header received from the server was 'Basic realm="gSOAP Web Service"'.***
When I try the same code from a test .net framework project it works fine.
Looking at the traffic via Fiddler, the .net core one is not setting the username and password at all from what I can tell. This is the Fiddler Auth header on the .net core call
No Proxy-Authorization Header is present.
Authorization Header is present: Basic Og==
Decoded Username:Password= :
Here is the code for the .net core version
var lm = new ListManagerService.lmapiSoapClient();
lm.ClientCredentials.UserName.UserName = "username";
lm.ClientCredentials.UserName.Password = "password";
Console.WriteLine("Current API Version: " + lm.ApiVersion());
and here is the same code that works fine on .net framework
var lm = new lmapiSoap.lmapi();
lm.Credentials = new System.Net.NetworkCredential("username", "password");
Console.WriteLine("Current API Version: " + lm.ApiVersion());
and the header in Fiddler
No Proxy-Authorization Header is present.
Authorization Header is present: Basic dXNlcm5hbWU6cGFzc3dvcmQ=
Decoded Username:Password= username:password
Now I have found a work around, elsewhere on SO, where you can add a header manually, but that seems to be a lot of work as every call needs to be wrapped in a using statement like this
using (new OperationContextScope(lm.InnerChannel))
{
// Add a HTTP Header to an outgoing request
var auth = "Basic " + Convert.ToBase64String(System.Text.Encoding.Default.GetBytes(lm.ClientCredentials.UserName.UserName + ":" + lm.ClientCredentials.UserName.Password));
var requestMessage = new HttpRequestMessageProperty();
requestMessage.Headers["Authorization"] = auth;
OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = requestMessage;
Console.WriteLine("Current API Version: " + lm.ApiVersion());
}
But there must be an easier way - given how simple this was in all previous versions of .NET
c#
asp.net
asp.net-core
soap-client
0 Answers
Your Answer