0

No matter whatever of combination of parameters I try, I always get a "The remote server returned an error: (401) Unauthorized" error.

I can get the token from postman through "Get New Access Token Button" without any problem (see attached image), but no success in getting it from vb.net

Public Shared Function GetInitializationToken() As String
    Try
        Dim StocktoolUser As String = "myuser"
        Dim StocktoolPasswd As String = "mypasswd"

        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
        ServicePointManager.ServerCertificateValidationCallback = AddressOf AcceptAllCertifications

        Dim request As WebRequest = WebRequest.Create("https://myazureserver/token")

        request.Headers.Add("Authorization", "Basic " & Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(StocktoolUser & ":" & StocktoolPasswd)))
        request.Method = "POST"
        request.ContentType = "application/x-www-form-urlencoded"
        request.ContentLength = 0

        Dim response As String = ""

        Using rs As Stream = request.GetResponse().GetResponseStream() **=> throws error**
            response = New StreamReader(rs, Encoding.UTF8).ReadToEnd()
        End Using

        Return JsonConvert.DeserializeObject(Of Dictionary(Of String, Object))(response)("token")
    Catch ex As Exception
        Throw ex
    End Try
End Function

Public Shared Function AcceptAllCertifications(ByVal sender As Object, ByVal certification As System.Security.Cryptography.X509Certificates.X509Certificate, ByVal chain As System.Security.Cryptography.X509Certificates.X509Chain, ByVal sslPolicyErrors As System.Net.Security.SslPolicyErrors) As Boolean
    Return True
End Function

Any help?

enter image description here

Diego Perez
  • 1,493
  • 1
  • 24
  • 43
  • This question [How do you use Basic Authentication with System.Net.Http.HttpClient?](https://stackoverflow.com/questions/58014360/how-do-you-use-basic-authentication-with-system-net-http-httpclient) might help you – JayV Nov 17 '21 at 21:19

0 Answers0