Get token using .net 6 code

Hi Everyone,

Have an issue getting the token from my .net 6 application. I am using the following sample code provided by Auth0:

var client = new RestClient("https://mydomain/oauth/token");
        var request = new RestRequest { Method = Method.Post };
        request.AddHeader("content-type", "application/json");
        request.AddParameter("application/json", "{\"client_id\":\"myclientid\",\"client_secret\":\"mysecret\",\"audience\":\"myaudience\",\"grant_type\":\"client_credentials\"}", ParameterType.RequestBody);
        var response = client.Execute(request);

but am getting a response with the following exception:

The message received was unexpected or badly formatted.
at System.Net.Security.SslStream.<ForceAuthenticationAsync>d__175`1.MoveNext()
   at System.Net.Security.SslStream.<ReplyOnReAuthenticationAsync>d__173`1.MoveNext()
   at System.Net.Security.SslStream.<ReadAsyncInternal>d__188`1.MoveNext()

I have tried enabling/disabling different security protocols for TLS, but with no luck. According to found articles it has to do with the incompatible cipher suite. Am I missing any configuration for that?

Was able to solve the problem, so just in case someone needs it, here is the code which works:

var handler = new HttpClientHandler();
handler.SslProtocols = System.Security.Authentication.SslProtocols.Tls12;

var httpClient = new HttpClient(handler);
var result = await httpClient.PostAsJsonAsync("https://mydomain/oauth/token",
            new TokenRequestDto
            {
                client_id = "myclientid",
                client_secret = "mysecret",
                audience = "myaudience",
                grant_type = "client_credentials"
            });
1 Like

Thanks for sharing it with the rest of community!