I am trying to access an ASP.NET Web API via c#. I wrote the API and I am able to access it and get a valid response using Postman. The API returns an access_token and refresh_token when presented with a username
, password
, and grant_type = password
.
Here is a screen capture of the response I receive when using Postman:
However, when I try and use the following C# code:
var userToValidate = new UserToValidate
{
UserName = "johndoe@mydomain.com",
Password = "Abc123!",
grant_type = "password"
};
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://localhost:4321");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = client.PostAsJsonAsync("oauth/token", userToValidate).Result;
content = response.Content.ReadAsStringAsync().Result;
}
I get an error ...
{"error":"unsupported_grant_type"}
I have got to be doing something wrong on the C# side of things. What am I missing?
P.S. Using .Result
because debugging async
code drives me nuts