I am using C# Xamarin for Visual Studio 2015 v4.2.2.11 (Xamarin.forms Portable project). I try to address a Webservice that requires authentication, but keep getting 401 Unauthorized.
The webservice is an Navision codeunit published as a webservice. It accepts a request XML and returns a response XML. I have used this way for many projects and never had a problem with authentication. Previous projects were .Net Windows, UWP, Windows mobile, Windows CE (compact framework) and recently a few projects written in WinDev, where I used the same communication.
The webservice requires an authentication in the form of a networkcredential (user,pwd,domain). In the UWP project, I can Add a Service Reference and authenticate this way. That works like a charm! In the Droid and iOS project, I cannot add a service ref, so I had to use a Web Reference. The Web Reference object does have a property Credentials, but is does not seem to be implemented?
From the Android emulator, I can browse to the default server page, but when I try to browse to the webservice, the browser also returns 401 nauthorized, no question for login or authentication. On the Windows 10 phone emulator, I can browse to the webservice page and the browser asks my credentials.
Because the UWP project uses Service ref and cannot use Web ref and the Android and iOS project both use Web Reference and cannot use service references, I had to pull the webservice connection to the platform projects.
I have a class SoapService where I declare the webservice as DynamicsNAV and the service itself is called serviceHandler and the action to perform is called ProcessRequests.
public class SoapService : ISoapService
{
DynamicsNAV.WebServiceHandler_PortClient serviceHandler;
string soapURL = "http://192.168.7.101:7047/DynamicsNAV90/WS/CRONUS%20BELGI%C3%8B%20NV/Codeunit/WebServiceHandler";
public SoapService()
{
serviceHandler = new DynamicsNAV.WebServiceHandler();
serviceHandler.Url = soapURL;
NetworkCredential nc = new NetworkCredential("wd", "wd", "VMNAV2016");
serviceHandler.Credentials = nc;
serviceHandler.PreAuthenticate = true;
}
...
In the SoapService, I have the public function GetDataAsync, I call the webservice and have tried this in multiple ways, all return 401... Try 1:
serviceHandler.ProcessRequests(request, ref response);
Exception thrown: StatusCode = System.Net.HttpStatusCode.Unauthorized
Try 2:
serviceHandler.ProcessRequestsAsync(request, response);
serviceHandler.ProcessRequestsCompleted += ServiceHandler_ProcessRequestsCompleted;
Exception thrown: StatusCode = System.Net.HttpStatusCode.Unauthorized
Try 3:
HttpWebRequest req = HttpWebRequest.CreateHttp(soapURL);
req.Method = "POST";
req.PreAuthenticate = true;
req.Credentials = new NetworkCredential("wd", "wd", "VMNAV2016");
byte[] postBytes = Encoding.ASCII.GetBytes(request);
req.ContentLength = postBytes.Length;
Stream postStream = req.GetRequestStream();
postStream.Write(postBytes, 0, request.Length);
postStream.Close();
WebResponse resp = req.GetResponse();
if (resp.ContentLength > 0)
{ }
Exception thrown: StatusCode = System.Net.HttpStatusCode.Unauthorized
... I tried adjusting the Service.cs, but nothing helped?!?
Can anyone help me please? I have to get this solved or a workaround...
Thanks in advance, Wim