Reputation: 9057
I am wondering how to convert that curl call below to a .NET Core HttpClient
call in C#:
curl -X POST \
-d "client_id=client-id-source" \
-d "audience=client-id-target" \
-d "subject_token=access-token-goes-here" \
--data-urlencode "grant_type=urn:ietf:params:oauth:grant-type:token-exchange" \
--data-urlencode "requested_token_type=urn:ietf:params:oauth:token-type:access_token" \
https://auth-dom/auth/realms/that-realm/protocol/openid-connect/token
So far I drafter something like:
public static class MultipartFormDataContentExtensions
{
public static void AddStringContent(this MultipartFormDataContent content, string name, string value)
{
content.Add(new StringContent(value), name);
}
}
public static class Program
{
public static async Task Main(params string[] args)
{
const string url = "https://auth-dom/auth/realms/that-realm/protocol/openid-connect/token";
var content = new MultipartFormDataContent();
content.AddStringContent("client_id", "client-id-source");
content.AddStringContent("audience", "client-id-source");
content.AddStringContent("subject_token", "access-token-goes-here");
var httpClient = new HttpClient();
var response = await httpClient.PostAsync(url, content);
}
}
But I have no idea how --data-urlencode
are converted in the request, any thoughts?
Upvotes: 3
Views: 2488
Reputation: 9057
That answer there helps: https://stackoverflow.com/a/21989124/4636721
I ended up just using a plain FormUrlEncodedContent
as content:
const string url = "https://auth-dom/auth/realms/that-realm/protocol/openid-connect/token";
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("subject_token", "access-token-goes-here"),
new KeyValuePair<string, string>("client_id", "client-id-source"),
new KeyValuePair<string, string>("audience", "client-id-target"),
new KeyValuePair<string, string>("client_id", "client-id-source"),
new KeyValuePair<string, string>("grant_type", "urn:ietf:params:oauth:grant-type:token-exchange"),
new KeyValuePair<string, string>("requested_token_type", "urn:ietf:params:oauth:token-type:access_token")
});
var httpClient = new HttpClient();
var response = await httpClient.PostAsync(url, content);
And it works just fine.
Upvotes: 5