How to write OAuth2 Web API Client in Asp.net MVC How to write OAuth2 Web API Client in Asp.net MVC asp.net asp.net

How to write OAuth2 Web API Client in Asp.net MVC


To support the client credentials grant type, your best option is probably to directly use HttpClient:

var request = new HttpRequestMessage(HttpMethod.Post, "http://server.com/token");request.Content = new FormUrlEncodedContent(new Dictionary<string, string> {    { "client_id", "your client_id" },    { "client_secret", "your client_secret" },    { "grant_type", "client_credentials" }});var response = await client.SendAsync(request);response.EnsureSuccessStatusCode();var payload = JObject.Parse(await response.Content.ReadAsStringAsync());var token = payload.Value<string>("access_token");

For interactive flows (like the authorization code flow), there are two better approaches: