在 .Net Core 3.1 应用程序中使用 Mongo Atlas API,但我无法HttpClient处理来自 Digest Authentication 的挑战。
该代码发送第一个请求,获得 401 响应,然后不通过正确的身份验证重新发送。
下面是我一直在努力工作的代码
var domain = "https://cloud.mongodb.com/";
var credCache = new CredentialCache();
credCache.Add(new Uri(domain),"Digest", new NetworkCredential(user,secret));
var httpClient = new HttpClient( new HttpClientHandler { Credentials = credCache});
var answer = await httpClient.GetAsync(new Uri($"{domain}api/atlas/v1.0/groups/{groupId}/databaseUsers"));
Run Code Online (Sandbox Code Playgroud)
这是我得到的回应
StatusCode: 401, ReasonPhrase: 'Unauthorized', Version: 1.1, Content: System.Net.Http.HttpConnectionResponseContent, Headers:
{
Date: Mon, 27 Jan 2020 21:03:14 GMT
WWW-Authenticate: Digest realm="MMS Public API", domain="", nonce="generatedNonce", algorithm=MD5, qop="auth", stale=false
Content-Type: application/json
Content-Length: 106
} …Run Code Online (Sandbox Code Playgroud)