Search code examples
c#microsoft-graph-apimicrosoft-graph-sdksmicrosoft-graph-files

Microsoft Graph - "System.ArgumentException: '"UTF-8"' is not a supported encoding name" only on my PC


I'm having an issue with what I've deduced is a content type with double quotes inside the single quotes when sending a request to Microsoft Graph. The "cool" part is that this only happens on my PC, and the exact same code with the exact same (databased) parameters runs fine on my colleague's PC.

The code is this:

var items = graph.Client
                .Users[graph.AuthenticationOptions.UserName]
                .Drive
                .Root
                .Children
                .Request()
                .GetAsync().Result
                .Where(_itm => (typeof(T).ToString().Contains("Folder") ? _itm.Folder != null : _itm.File != null));

Everything works fine up to the .Result, but looking at the stack trace tells me that it's actually failing when sending the request with the following error:

Result Message:
Test method XXX.FileTransfer.Test.LibaryTests.MSGraphTests.OneDriveClient_FileNotNull threw exception:
System.AggregateException: One or more errors occurred. ---> Microsoft.Graph.ServiceException: Code: generalException
Message: An error occurred sending the request.
---> Azure.Identity.AuthenticationFailedException: ClientSecretCredential authentication failed: The character set provided in ContentType is invalid. Cannot read content as string using an invalid character set. ---> System.InvalidOperationException: The character set provided in ContentType is invalid. Cannot read content as string using an invalid character set. ---> System.ArgumentException: '"UTF-8"' is not a supported encoding name. For information on defining a custom encoding, see the documentation for the Encoding.RegisterProvider method.

So, I have seen the part at the end about using Encoding.RegisterProvider, but it doesn't seem to work on .NET Framework 4.8 applications? Unless I just misunderstood the docs.

From a very extensive search through good old Google, I found a few references to this same error but sadly they were on different webservices, and rather annoyingly a couple of the companies behind the APIs were able to add the double quoted UTF-8 to their accepted response types.

I have checked the formatting of the keys, etc. and nothing seems out of place. I have even tried manually setting the content type in the headers of the request:

Microsoft.Graph.BaseRequest req =  (Microsoft.Graph.BaseRequest)graph.Client
                .Users[graph.AuthenticationOptions.UserName]
                .Drive
                .Root
                .Children
                .Request();

            req.ContentType = "application/json; charset=utf-8";
            req.Headers.Add(new HeaderOption("Accept", "application/json; charset=utf-8"));
            var result = ((IDriveItemChildrenCollectionRequest)req).GetAsync().Result;

with no change in behaviour.

I'm very much at a loss of what to do here. Clearly there is something wrong with the configuration of my PC if it's sending this as the default content type, but I have no idea what. Any help would be greatly appreciated.

The behaviour is the same across the Sharepoint, OneDrive, and Mail APIs.


Solution

  • For some reason, this is now working.

    I have changed nothing and now the problem does not occur. However!

    The company I work for has had new VPN hardware and dropped the proxy server, and that seems to coincide with the MSGraph API now suddenly working. So my best answer for anyone who finds this question is to investigate whether your VPN or proxy server is modifying your content type in any way.

    I will add this link for further reading, which explains the effect your proxy server may have on a HTTP Request: Do HTTP proxy servers modify request packets?