locked
WebService authentication issue RRS feed

  • Question

  • 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

    Wednesday, August 8, 2018 9:47 AM

All replies

  • Just add a header to the Invoke-WebRequest call.

    help Invoke-WebRequest -Parameter Headers


    \_(ツ)_/

    Wednesday, August 8, 2018 11:51 AM
  • Thanks for the reply.

    The PowerShell functions currently use New-WebServiceProxy:

    $Proxy_test = New-WebServiceProxy -Uri $URI_test -Namespace WebServiceProxy.test

    The request is then built and sent.  I cant see an option for adding headers here.

    Thanks

    Paul

    Wednesday, August 8, 2018 12:34 PM
  • help New-WebServiceProxy -para Credential

    \_(ツ)_/

    Wednesday, August 8, 2018 12:58 PM
  • Using -Credential also does not help.  As i understand it should use basic authentication but for some reason the server is not.  This is why we added the above code into the Visual Studio project which forced it to use basic authentication.
    Wednesday, August 8, 2018 2:57 PM
  • Then you cannot use new-webserviceproxy.


    \_(ツ)_/

    Wednesday, August 8, 2018 3:04 PM