Auth0 Home Blog Docs

Extract Management API JWT token from response

Hello,
I’m getting a 200 status from the code below, but not able to find the JWT token in the response. I’m somewhat new to .NET Core’s HttpClient, would appreciate help on extracting the jwt token using .NET

HttpClient client = new HttpClient();
string url = "https://MYDOMAIN.auth0.com/oauth/token";
string body = "{\"client_id\":\"957...MYIDTOKEN...wRU\",\"client_secret\":" +
                "\"zbV...MYSECRETTOKEN...tuj\"," +
                "\"audience\":\"https://MYDOMAIN.auth0.com/api/v2/\",\"grant_type\":\"client_credentials\"}";
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, url);
client.DefaultRequestHeaders.Accept
  .Add(new MediaTypeWithQualityHeaderValue("application/json")); //ACCEPT header
            
request.Content = new StringContent(body,Encoding.UTF8, "application/json");
            
// This returns 200 OK
HttpResponseMessage response = await client.PostAsync(url, request.Content);
// Can't see the jwt token in either of these.
HttpContent content = response.Content;
HttpResponseHeaders header = response.Headers;

Would be good to know what the content looks like. Maybe you can provide an example for a dummy test API that isn’t critical to show / doesn’t actually do anything, or remove anything sensitive, if it’s not encoded output. What format is the response, Json?

I figured it out. The response.Content doesn’t actually contain the content, it’s just the metadata for it. To get the content (the Auth0 Management token) I needed the following:

// starting where I left off above....
request.Content = new StringContent(body, Encoding.UTF8, "application/json");
HttpResponseMessage response =  await Client.PostAsync(url, request.Content);
JObject jsonResult = response.Content.ReadAsAsync<JObject>().Result;
string auth0MgtToken = jsonResult.Value<string>("access_token");
1 Like

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.