Using HTTP to get a token from IdentityServer 4.
This is part 2, part 1 can be found here.
IdentityServer's token endpoint expects to receive content as application/x-www-form-urlencoded.
Note: if you wish to view the request and response in fiddler use localhost.fiddler
Create a console application select .Net Core as the type.
Optionally add a class if you want to deserialize the data to an Object.
public class RootObject
{
public String access_token { get; set; }
public String expires_in { get; set; }
public String token_type { get; set; }
}
Example 1 using WebClient
Create a NameValueCollection
var body = new NameValueCollection()
{
{ "client_id", "client" },
{"client_secret", "secret" },
{"grant_type", "client_credentials" },
{"scope", "api1" }
};
Call the token endpoint
using (WebClient webclient = new WebClient())
{
byte[] response =
webclient.UploadValues("http://localhost:5000/connect/token", body);
var result = Encoding.UTF8.GetString(response);
var json = JsonConvert.DeserializeObject<RootObject>(result);
var token = json.access_token;
}
Example 2 using HttpClient
Create a list of key/value pairs
List<KeyValuePair<string, string>> keyValues = new List<KeyValuePair<string, string>>()
{
new KeyValuePair<string, string>("client_id", "client"),
new KeyValuePair<string, string>("client_secret", "secret"),
new KeyValuePair<string, string>("grant_type", "client_credentials"),
new KeyValuePair<string, string>("scope", "api1")
};
Call the endpoint
using (HttpClient client = new HttpClient())
{
var url = "http://localhost:5000/connect/token";
HttpContent content = new FormUrlEncodedContent(keyValues);
var result = client.PostAsync(url, content).Result;
string resultContent = result.Content.ReadAsStringAsync().Result;
var statusCode = result.StatusCode;
var json = JsonConvert.DeserializeObject<RootObject>(resultContent);
var token = json.access_token;
}
Here's the result.
Program.cs
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Net;
using System.Net.Http;
using System.Text;
namespace HttppClient
{
class Program
{
public static void Main()
{
var body = new NameValueCollection()
{
{ "client_id", "client" },
{"client_secret", "secret" },
{"grant_type", "client_credentials" },
{"scope", "api1" }
};
using (WebClient webclient = new WebClient())
{
string result = string.Empty;
byte[] response =
webclient.UploadValues("http://localhost:5000/connect/token", body);
result = Encoding.UTF8.GetString(response);
var json = JsonConvert.DeserializeObject(result);
var token = json.access_token;
}
List> keyValues = new List>()
{
new KeyValuePair("client_id", "client"),
new KeyValuePair("client_secret", "secret"),
new KeyValuePair("grant_type", "client_credentials"),
new KeyValuePair("scope", "api1")
};
using (HttpClient client = new HttpClient())
{
var url = "http://localhost:5000/connect/token";
HttpContent content = new FormUrlEncodedContent(keyValues);
var result = client.PostAsync(url, content).Result;
string resultContent = result.Content.ReadAsStringAsync().Result;
var statusCode = result.StatusCode;
var json = JsonConvert.DeserializeObject(resultContent);
var token = json.access_token;
}
}
}
}
public class RootObject
{
public String access_token { get; set; }
public String expires_in { get; set; }
public String token_type { get; set; }
}
Comments
Post a Comment