如何使用 Xamarin ModernHttpClient 或 System.Net.Http.httpClient 进行 NTLM 身份验证

How to do NTLM Authentication with Xamarin ModernHttpClient or System.Net.Http.httpClient

我在 Xamarin 应用程序中使用此 httpClient:

var httpClient = new HttpClient(new NativeMessageHandler());

我的服务器需要 NTLM 身份验证。我相信我必须做这样的事情:

httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("NTLM",...);

但我不清楚应该如何替换“...”。 AuthenticationHeaderValue 的文档没有说明任何内容。

这是正确的方法吗?我必须做什么?

对于 .NET Standard 1.4 库中的 Android/iOS/UWP 使用 System.Net.Http.httpClient 在 Xamarin 项目中使用:(我还没有使用 ModernHttpClient

// Note: The NTLM domain is important here, otherwise basic auth will be used:
var credentials = new NetworkCredential("username", "password", "domain");

var handler = new HttpClientHandler { Credentials = credentials, UseDefaultCredentials = false }

var client = new HttpClient(handler);

client.DefaultRequestHeaders.Authorization可以不用管,因为上面的代码会在内部为每个请求生成授权头。