A small and simple task, but which took me a lot of time searching the web. So now I know where to find it.
Disclaimer we will be using the Password Grant Flow you can find details here, in the link its discouraged to use this flow except maybe on legacy applications.
That aside.
Disclaimer we will be using the Password Grant Flow you can find details here, in the link its discouraged to use this flow except maybe on legacy applications.
That aside.
Identity Server 4
Set up you identity server to use the password grant as indicated in their documentation, you can find link here .
C# Code
var client = new HttpClient();
client.BaseAddress = new Uri("http://localhost:5000");
var byteArray = Encoding.Default.GetBytes("ro.client:secret");
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
var request = new HttpRequestMessage(HttpMethod.Post, "/connect/token");
var keyValues = new List<KeyValuePair<string, string>>();
keyValues.Add(new KeyValuePair<string, string>("UserName", "alice"));
keyValues.Add(new KeyValuePair<string, string>("Password", "password"));
keyValues.Add(new KeyValuePair<string, string>("Scope", "api1"));
keyValues.Add(new KeyValuePair<string, string>("grant_type", "password"));
request.Content = new FormUrlEncodedContent(keyValues);
var response = await client.SendAsync(request);
client.BaseAddress = new Uri("http://localhost:5000");
var byteArray = Encoding.Default.GetBytes("ro.client:secret");
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
var request = new HttpRequestMessage(HttpMethod.Post, "/connect/token");
var keyValues = new List<KeyValuePair<string, string>>();
keyValues.Add(new KeyValuePair<string, string>("UserName", "alice"));
keyValues.Add(new KeyValuePair<string, string>("Password", "password"));
keyValues.Add(new KeyValuePair<string, string>("Scope", "api1"));
keyValues.Add(new KeyValuePair<string, string>("grant_type", "password"));
request.Content = new FormUrlEncodedContent(keyValues);
var response = await client.SendAsync(request);
Comments
Post a Comment