.NET 5 HttpClient Digest Authentication

2.2k views Asked by At

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?

0

There are 0 answers