Android application type
.NET Android (net7.0-android, etc.)
Affected platform version
.NET 7 Current
Description
Transferred from dotnet/runtime#81839
The basic authentication via HTTPClientHandler credentials doesn’t work for Android (.net 7.0).
Can you confirm, this is a bug or is something wrong with the coding below?
Thanks,
Michael
Steps to Reproduce
- Create MAU project .net 7.0
- add the following coding:
HttpClientHandler lClientHandler = new HttpClientHandler();
CancellationTokenSource lCancel = new CancellationTokenSource();
lCancel.CancelAfter(TimeSpan.FromMilliseconds(5000));
lClientHandler.Credentials = new NetworkCredential("<user>", "<password>");
var lClient = new HttpClient(lClientHandler);
var lResponse = await lClient.GetAsync("https://<web address>" , HttpCompletionOption.ResponseContentRead, lCancel.Token).ConfigureAwait(false);
This returns a 401 (Unauthorized) for android.
The same code works just fine for windows. I have tested the problem with multiple android emulators (Android 11 and Android 13) and an android device (Android 13). The web service is accessible via browser on the device (and the login/password works as well).
Did you find any workaround?
If I set the Authorization manually it works just fine.
(Instead of:
lClientHandler.Credentials = new NetworkCredential("<user>", "<password>");
for instance use:
var byteArray = Encoding.ASCII.GetBytes("<user>:<password>");
lClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
)
However this workaround doesn’t really work for me, because the code is part of a library implementation I use and which I cannot change. Furthermore this coding works in a Xamarin Android app (non MAUI) without problem.
Relevant log output
No response
Android application type
.NET Android (net7.0-android, etc.)
Affected platform version
.NET 7 Current
Description
Transferred from dotnet/runtime#81839
The basic authentication via HTTPClientHandler credentials doesn’t work for Android (.net 7.0).
Can you confirm, this is a bug or is something wrong with the coding below?
Thanks,
Michael
Steps to Reproduce
This returns a 401 (Unauthorized) for android.
The same code works just fine for windows. I have tested the problem with multiple android emulators (Android 11 and Android 13) and an android device (Android 13). The web service is accessible via browser on the device (and the login/password works as well).
Did you find any workaround?
If I set the Authorization manually it works just fine.
(Instead of:
lClientHandler.Credentials = new NetworkCredential("<user>", "<password>");for instance use:
)
However this workaround doesn’t really work for me, because the code is part of a library implementation I use and which I cannot change. Furthermore this coding works in a Xamarin Android app (non MAUI) without problem.
Relevant log output
No response