0

I am trying to get a bearer token from an identity server using the WebRequest Class because the program has to work with .NET 2.0, and the port from where I am getting the token is 10000.

I tried creating the WebRequest like

  • identityURL:10000/getToken
  • http://identityURL:10000/getToken

but neither one works. The first returns an "Unkown URL Prefix"-Error and the second one a "400 Bad Request"-Error.

Is there any other way to get the token under .NET 2.0 ?

Thank you very much in advance for your help.

-Simon

Edit:

Using wc As New WebClient()
      Dim postData As String = "grant_type=" + sTokenGrantType + "&username=" + sIdentityServerClientName + "&password=" + sIdentityServerClientSecret + "&scope=Api"
      Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)
      Dim responseArray As Byte()

      wc.Headers.Add(HttpRequestHeader.ContentType, "application/x-www-form-urlencoded")
      wc.Headers.Add(HttpRequestHeader.ContentLength, byteArray.Length.ToString)
      wc.Headers.Add(HttpRequestHeader.UserAgent, "User-Agent: PostmanRuntime/7.15.0")

      wc.BaseAddress = sIdentityServerURL

      responseArray = wc.UploadData("/getToken", "POST", byteArray)

      MsgBox(responseArray)
End Using

I tried using the WebClient class but that results in the following error: "An exception occurred during a WebClient request."

SOLVED (see below)

s137
  • 41
  • 1
  • 10
  • You can create a `WebRequest` from a `Uri` and the class has a `Port` property. – jmcilhinney Jul 08 '19 at 07:10
  • Just realised that `Port` is read-only so the value would come from the URL anyway. – jmcilhinney Jul 08 '19 at 07:11
  • Are you working with OAuth or OAuth2 for the Bearer token? – 42LeapsOfFaith Jul 08 '19 at 07:18
  • I didn't set it up unfortunately, so I don't know, but to the best of my knowledge we are using OAuth2 – s137 Jul 08 '19 at 07:35
  • I have done both OAuth and OAuth2 with VB.Net 2.0. They are both VERY different. The port number is usually automatically handled by the WebRequest(). Note: OAuth2 is https only. I may be able help more if you find out which one you are using. – 42LeapsOfFaith Jul 08 '19 at 09:00
  • Alright, thank you, I will find that out. In the meantime, I tried something else and added it to the original Question. It would be great if you could take a look at it. – s137 Jul 08 '19 at 09:47
  • @jmcilhinney Passing the original `Uri` to a `UriBuilder`, you can set the `Port` property. Or any other value that is read-only in the `Uri` class. – Jimi Jul 08 '19 at 13:43

2 Answers2

0

I searched over internet and following seems to work. I haven't tried it yet but I found following code reference at several websites.

Uri myUri = new Uri("http://{server}:{port}");
WebRequest.Create(Uri);

Also, check if the ServicePoint.BindIPEndPointDelegate callback if of help. Sample code :

public static IPEndPoint BindIPEndPointCallback(ServicePoint servicePoint, IPEndPoint remoteEndPoint, int retryCount)
{
    Console.WriteLine("BindIPEndpoint called");
      return new IPEndPoint(IPAddress.Any,5000);

}

public static void Main()
{

    HttpWebRequest request = (HttpWebRequest) WebRequest.Create("http://MyServer");

    request.ServicePoint.BindIPEndPointDelegate = new BindIPEndPoint(BindIPEndPointCallback);

    HttpWebResponse response = (HttpWebResponse)request.GetResponse();

}

This is taken from here. Try changing the port number in line 3.

Sukhi
  • 13,261
  • 7
  • 36
  • 53
0

Thanks for all your help, but I solved it myself, by using the WebClient instead of a WebRequest. This is how I did it:

The code is slightly different than the one posted above. The exception I got with the WebClient occured because of an issue with one of the HTTP-Headers, that I set.

Using wc As New WebClient()
      Dim postData As String = "grant_type=" + sTokenGrantType + "&client_id=" + sIdentityServerClientName + "&client_secret=" + sIdentityServerClientSecret + "&scope=Api"
      Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)
      Dim responseArray As Byte()

      wc.Headers.Add(HttpRequestHeader.ContentType, "application/x-www-form-urlencoded")

      wc.BaseAddress = sIdentityServerURL

      responseArray = wc.UploadData("/getToken", "POST", byteArray)
End Using
s137
  • 41
  • 1
  • 10