Author Posts

August 8, 2018 at 11:51 am


Hi

I have been having issues connecting to an internal webservice – 401 Unauthorized.  I get this connecting from PowerShell and from a Project created using Visual Studio.

For some reason it looks like basic authentication is not being used so adding the following in the Visual Studio Project (Reference.cs created from wsdl file) works:

public partial class testServiceNoKeepAlive : testService
   {
       protected override System.Net.WebRequest GetWebRequest(Uri uri)
       {
           System.Net.HttpWebRequest request;
           request = (System.Net.HttpWebRequest)base.GetWebRequest(uri);
           request.KeepAlive = false;

           System.Net.NetworkCredential networkCredentials = Credentials.GetCredential(uri, "Basic");

           if (networkCredentials != null)

           { byte[] credentialBuffer = new UTF8Encoding().GetBytes(networkCredentials.UserName + ":" + networkCredentials.Password); request.Headers["Authorization"] = "Basic " + Convert.ToBase64String(credentialBuffer); }

           else

           { throw new ApplicationException("No network credentials"); }

           return request;
       }

   }

Can something similar be done in PowerShell?

Thanks

Paul