.NET 5.0 vs Framework 4.8 HTTPRequest oAuth02.

megabug

Member
Joined
Feb 15, 2022
Messages
11
Programming Experience
10+
Hi everybody,

I'm working on an app written in .net Framework 4.8 with usage of an API that use OAuth02 to authenticate. And I'm not really familiar with web API in C#.
The sample code I received is based on .NET 5.0 and work perfectly. However, when I look at the compatibility of the commands used,
I saw it should be ok to use the same code. However, I received an error 400. I tried to use fiddler to see the differences in the message sent
and seem to be content length. I tried to adjust it and now I received an other message that seem to be related to async function.
I tried to reference the .net 5 code as a new DLL but it seem not possible du to the target framework of my app (Framework 4.8) So I need to understand what is the difference between .NET 5.0 and Framwork 4.8 call.
I supposed It should be something related to TLS security or something like that, but How to manage it with old framework.

Could someone help me to downgrade that code.

Thanks in advance

Here is .NET 5.0 code I received.
C#:
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
namespace TestMemento2
{
    class Program
    {
        private static readonly HttpClient client = new HttpClient();
        static async Task Main(string[] args)
        {

            string clientID = "abc";
            string clientSecret = "1234";
            string url = "url";

            var values = new List<KeyValuePair<string, string>>();
            values.Add(new KeyValuePair<string, string>("grant_type", "client_credentials"));
            var content = new FormUrlEncodedContent(values);

            string encodedAuth = Convert.ToBase64String(Encoding.Default.GetBytes(clientID + ":" + clientSecret));
            var requestMessage = new HttpRequestMessage(HttpMethod.Post, url);
            requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Basic", encodedAuth);
            requestMessage.Content = content;
        
            //make the request
            var task = client.SendAsync(requestMessage);
            var response = task.Result;
            response.EnsureSuccessStatusCode();
            string responseBody = response.Content.ReadAsStringAsync().Result;
            Console.WriteLine(responseBody);
            Console.ReadLine();
        }

    }
}
 
Last edited:

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
5,251
Location
Chesapeake, VA
Programming Experience
10+
Interesting... And likely the reason why I couldn't reproduce the problem is because I'd hacked my registry to make UTF8 my default code page while trying to make Console output support emojis.
 
Top Bottom