1
votes

I need to access a Portal which is controlled with Bearer Authentication. Client needs to obtain authentication token then add it to each request

URL ~/token Method POST Content-Type application/x-www-form-urlencoded Payload grant_type=password&username=UserName&password=Password

**How do i add the payload which includes the grant type and username and password to my code **

so far my code is as follows :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;

 namespace ConsoleRestClient
 {
     class Program
    {
    static void Main(string[] args)
    {
        string URI = "https://token";
        WebRequest request = WebRequest.Create(URI);
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";

       }
     }`
   }
2

2 Answers

0
votes

You should be sending your request with content type "application/json" and put requested login variable on the body. To give you an idea:

  • Headers:

    Content-Type:application/json

  • Body:

    { "username": "MyUserName", "password": "MyPassword" }

0
votes

Probably you can try adding to the request headers, by doing something like this:

string URI = "https://token";
WebRequest request = WebRequest.Create(URI);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.headers.Add(<header_name>, <header_value>);

then you read these headers on your api.