16

I wrote a HTTP request in Postman and I want to write the same request in my application. There is an option in postman to see the code of the request for C#. In postman it shows request using RestSharp, since I don't want to use external NuGet packages in my project I'm trying to write the same request with objects from .NET Framework.

The RestSharp code looks like this:

var client = new RestClient("https://login.microsoftonline.com/04xxxxa7-xxxx-4e2b-xxxx-89xxxx1efc/oauth2/token");
var request = new RestRequest(Method.POST);       
request.AddHeader("Host", "login.microsoftonline.com");            
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddParameter("undefined", "grant_type=password&client_id=6e97fc60-xxx-445f-xxxx-a9b1bbc9eb2d&client_secret=4lS*xxxxxYn%5BENP1p%2FZT%2BpqmqF4Q&resource=https%3A%2F%2Fgraph.microsoft.com&username=myNameHere%402comp.onmicrosoft.com&password=xxxxxxxxxx6", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);

enter image description here

I tried to write the same request with HttpWebRequest:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://login.microsoftonline.com/0475dfa7-xxxxxxxx-896cf5e31efc/oauth2/token");
request.Method = "GET";
request.Referer = "login.microsoftonline.com";
request.ContentType = "application/x-www-form-urlencoded";

request.Headers.Add("grant_type", "password");
request.Headers.Add("client_id", "6e97fc60-xxxxxxxxx-a9bxxxxxb2d");
request.Headers.Add("client_secret", "4lSxxxxxxxxxxxmqF4Q");
request.Headers.Add("resource", "https://graph.microsoft.com");
request.Headers.Add("username", "xxxx@xxxxx.onmicrosoft.com");
request.Headers.Add("password", "xxxxxxxxxxxxx");

HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync();

but I'm getting HTML content I think that I need to add the parameters not as header, how can this be achieved?

Codey
  • 487
  • 1
  • 8
  • 27
  • Possible duplicate of [Setting a WebRequest's body data](https://stackoverflow.com/questions/4256136/setting-a-webrequests-body-data) – madoxdev Jun 30 '19 at 10:13
  • Use a sniffer like wireshark or fiddler and compare the sniffer results with postman and c#. The default header in Net library are probably different from postman and that is why you are getting different results. The Content-Type is probably that main reason for the differences and I would add to your c# code. – jdweng Jun 30 '19 at 10:14
  • You do not need to set it as a header. It's explicitly stated in the documentation and example here: https://learn.microsoft.com/en-us/graph/auth-v2-service - Read carefully and don't simply assume things. – Zimano Sep 08 '21 at 14:38

1 Answers1

30

I would not use WebRequest if you are able to, rather use HttpClient:

var req = new HttpRequestMessage(HttpMethod.Get, "https://login.microsoftonline.com/0475dfa7-xxxxxxxx-896cf5e31efc/oauth2/token");
req.Headers.Add("Referer", "login.microsoftonline.com");
req.Headers.Add("Accept", "application/x-www-form-urlencoded");
req.Headers.Add("Content-Type", "application/x-www-form-urlencoded");

// This is the important part:
req.Content = new FormUrlEncodedContent(new Dictionary<string, string>
{
    { "grant_type", "password" },
    { "client_id", "6e97fc60-xxxxxxxxx-a9bxxxxxb2d" },
    { "client_secret", "4lSxxxxxxxxxxxmqF4Q" },
    { "resource", "https://graph.microsoft.com" },
    { "username", "xxxx@xxxxx.onmicrosoft.com" },
    { "password", "xxxxxxxxxxxxx" }
});

HttpResponseMessage resp = await httpClient.SendAsync(req);

// Work with resp
Tobias Tengler
  • 6,848
  • 4
  • 20
  • 34
  • Thank's it works great, I just needed to change to `httpClient.BaseAddress = new Uri("https://graph.microsoft.com"); httpClient.DefaultRequestHeaders .Accept .Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));` – Codey Jun 30 '19 at 10:45
  • @Codey I'm glad I could help you solve your issue, but since *https://login.microsoftonline.com/0475dfa7-xxxxxxxx-896cf5e31efc/oauth2/token* is an absolute URL, setting the `httpClient.BaseAddress` shouldn't make a difference for this request and thus can be removed. – Tobias Tengler Jun 30 '19 at 10:49
  • @TobiasTengler what if your content is a MutliPart form? How would you then attach parameters to the `HttpClient`? – SpiritBob Feb 19 '21 at 13:19
  • Create a `new MultipartContent()` and then call `.Add` with your other Content, e.g. `multiPartContent.Add(new StringContent(bla));`. just search for MultipartContent exmaples and you should find something. – Tobias Tengler Feb 19 '21 at 13:30