3

I'm trying to use HttpClient to do digest authentication in .NET 5 to work with mongoDB Atlas. The workaround suggested in .Net Core HttpClient Digest Authentication no longer works for .NET 5.

for example, this code

AppContext.SetSwitch("System.Net.Http.UseSocketsHttpHandler", false);
var httpClient = new HttpClient(
            new HttpClientHandler
            {
                Credentials =
                    new NetworkCredential(
                        userName,
                        password)
            });
var domain = "https://cloud.mongodb.com/";
var response = httpClient.GetAsync(new Uri($"{domain}api/atlas/v1.0/groups/{groupId}")).Result;
        

return 200 ok with the correct data when the target framework is .net core 3.1

but when the target framework is .NET 5 http client don't handle the digest flow behind the scenes and returns

StatusCode: 401, ReasonPhrase: 'Unauthorized', Version: 1.1, Content: System.Net.Http.HttpConnectionResponseContent, Headers:
{
  WWW-Authenticate: Digest realm="MMS Public API", domain="", nonce=""generatedNonce"", algorithm=MD5, qop="auth", stale=false
  x-envoy-upstream-service-time: 2
  Date: Sun, 17 Jan 2021 13:37:12 GMT
  Server: envoy
  Content-Type: application/json
  Content-Length: 106
} 

does anyone know if I have any alternative besides implementing the digest flow by myself?

Amir Harari
  • 121
  • 12
  • 1
    Are you using http or https (secure)? Youcould be getting an unauthorized response if you are sending http and server wants https. Or if the UserAgent is different. So are you using the same machine on the client and the same user. Trying to find out if you just want from Core 3.1 to Core 5.0 on same machine or other things also changed. See following : https://learn.microsoft.com/en-us/dotnet/core/compatibility/5.0 – jdweng Jan 17 '21 at 14:59
  • There is a bug in `System.Net.Http`. You can track it here: https://github.com/dotnet/runtime/issues/50283 – Artur Mar 26 '21 at 21:27

0 Answers0