Getting a Management API token for .NET

I’m posting this because there were a lot of little things I had to figure out on my own to get the management api to work in .NET.

  1. The .NET Management SDK doesn’t work, at least in .NET 7. I won’t speculate as to why. All I know is if I make a request manually to the end point, it works. If I use the SDK I get a “No Such Host is Found error”
  2. Here is how you configure a request for a token in .NET, since the document uses an outdated nuget package.
private async Task<ManagementToken> GetToken()
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "<<YOUR DOMAIN>>/oauth/token");
                var contentHeader = new MediaTypeHeaderValue("application/json") { CharSet = Encoding.UTF8.WebName };
                var content = new ManagementTokenRequestContent()
                    client_id = "<<YOUR CLIENT ID FOR THE API EXPLORER APPLICATION>>",
                    client_secret = "<<YOUR CLIENT SECRET FOR THE API EXPLORER APPLICATION>>",
                    audience = "<<YOUR IDENTITIFER FOR THE AUTH0 MANAGEMENT API>>",
                    grant_type = "client_credentials"

                request.Content = JsonContent.Create(content, contentHeader);
                var response = await _httpClient.SendAsync(request);
		var token = await response.Content.ReadFromJsonAsync<ManagementTokenResponse>();
                var managementToken = new ManagementToken()
                    Token = token.Token,
                    ExpirationTime = DateTime.UtcNow.AddHours(23),
                return managementToken;

    public class ManagementTokenRequestContent
        public string client_id { get; set; } = string.Empty;

        public string client_secret { get; set; } = string.Empty;

        public string audience { get; set; } = string.Empty;

        public string grant_type { get; set; } = string.Empty;

    public class ManagementTokenResponse
        public string Token { get; set; } = string.Empty;
        public string TokenType { get; set; } = string.Empty;

Thanks for posting this solution! Let us know if you have any questions.

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