dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.27k stars 1.76k forks source link

HttpClientHandler ClientCertificate property is null on Android #8860

Closed GarageGadget closed 2 years ago

GarageGadget commented 2 years ago

Description

The application I am building requires the use of Client X509 certificates. I am trying to add the client certificate to the HttpClientHandler, however the ClientCertificate property is null and therefore throwing an null reference exception when calling the Add() method with a X509 certificate.

Snippet

  var handler = new HttpClientHandler();
  handler.SslProtocols = System.Security.Authentication.SslProtocols.Tls12 | System.Security.Authentication.SslProtocols.Tls13;
  handler.ClientCertificateOptions = ClientCertificateOption.Manual;
  if(handler.ClientCertificates == null)
  {
      Console.WriteLine("ClientCertificates is null");
  }
  // handler.ClientCertificates.Add(clientCert); <-- Failure

Using Microsoft Visual Studio Enterprise 2022 (64-bit) - Preview Version 17.3.0 Preview 4.0

Steps to Reproduce

  1. Create a Maui App
  2. Add code similar to snippet above in a button handler or any suitable method.
  3. Run and debug the Maui App on Android
  4. The ClientCertificates property will be null and log the message to the console.

Version with bug

6.0.400

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

Android 9

Did you find any workaround?

No response

Relevant log output

No response

jonathanpeppers commented 2 years ago

@steveisok @simonrozsival would you know if this is expected behavior?

Should we initialize this property with an empty collection?

https://github.com/xamarin/xamarin-android/blob/ace5f71c40d05070612cb860f38764eae3deef59/src/Mono.Android/Xamarin.Android.Net/AndroidMessageHandler.cs#L150

@GarageGadget for now you can do this as a workaround?

handler.ClientCertificates ??= new ();
ghost commented 2 years ago

Hi @GarageGadget. We have added the "s/needs-info" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

simonrozsival commented 2 years ago

We had a similar report from a different customer recently, here's the issue in Xamarin.Android: https://github.com/xamarin/xamarin-android/issues/7274

I suggest closing this issue as it's specific to Xamarin.Android.