Skip to content

HttpClient(AndroidMessageHandler) with NTLM v2 auth and Self Signed Certificate returns 401 Unauthorized #110523

@AzizBelAbed

Description

@AzizBelAbed

Description

I'm using an on-premise exchange server with NTLM v2 authentication.
When attempting to authenticate using HttpClient and AndroidMessageHandler, I encounter a 401 Unauthorized error, specifically due to a self-signed certificate. Interestingly, when using a public certificate, authentication proceeds without any issues.

Old ticket: #102107
Issue still reproducible using .NET9

Here parts of the code:

var _httpClientHandler = new CustomAndroidMessageHandler();
var _credentials = new CredentialCache
{
{ new Uri(serverEndpoint), "NTLM", new NetworkCredential(emailEntry.Text, passwordEntry.Text)}
};
_httpClientHandler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => { return true; };
_httpClientHandler.Credentials = _credentials;
HttpClient httpclient = new HttpClient(_httpClientHandler);
public class CustomAndroidMessageHandler : AndroidMessageHandler
{
protected override async Task WriteRequestContentToOutput(
HttpRequestMessage request,
HttpURLConnection httpConnection,
CancellationToken cancellationToken)
{
var stream = await request.Content.ReadAsStreamAsync().ConfigureAwait(false);
await stream.CopyToAsync(httpConnection.OutputStream!, 4096, cancellationToken).ConfigureAwait(false);

if (stream.CanSeek)
{
stream.Seek(0, SeekOrigin.Begin);
}
}
}

I added

<AndroidUseNegotiateAuthentication>true</AndroidUseNegotiateAuthentication>

in csproj

Reproduction Steps

.

Expected behavior

Working.

Actual behavior

401 Unauthorized.

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions