2

I would like to use a certificate in my gRPC client. I am trying doing this:

public ServiceClientGrpc(string paramDireccion, int paramPuerto, string paramCertificado, string paramKey)
        {
            var cert = X509Certificate2.CreateFromPem(paramCertificado, paramKey);


            var handler = new HttpClientHandler();
            handler.ClientCertificates.Add(cert);
            HttpClient httpClient = new(handler);


            var channel = GrpcChannel.ForAddress(paramDireccion + ":" + paramPuerto, new GrpcChannelOptions
            {
                HttpClient = httpClient
            });


            _client = new Greeter.GreeterClient(channel);
        }

My problem is in this two lines:

var handler = new HttpClientHandler();
handler.ClientCertificates.Add(cert);

handler is not null, but the collecion ClientCertificates is null, so I get an exception when I try to add the certificate.

According to the Microsfot documentantion, it is the way to do it.

public Ticketer.TicketerClient CreateClientWithCert(
    string baseAddress,
    X509Certificate2 certificate)
{
    // Add client cert to the handler
    **var handler = new HttpClientHandler();
    handler.ClientCertificates.Add(certificate);**

    // Create the gRPC channel
    var channel = GrpcChannel.ForAddress(baseAddress, new GrpcChannelOptions
    {
        HttpHandler = handler
    });

    return new Ticketer.TicketerClient(channel);
}

How could I add the certificates to my handler?

Thanks.

EDIT: I create a WPF project which target is .NET 6.0 too with one button. The code.behind is this:

public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); }

private void Button_Click(object sender, RoutedEventArgs e)
{
    System.Net.Http.HttpClientHandler miHandler = new System.Net.Http.HttpClientHandler();
}

}

When I set a breakpoint at the end of the method, I can see that ClientCertificates is not null.

EDIT 2: So the problem is in my MAUI project, when I debug in my phone, the collection is null, but if I use the same library in my WPF project it works.

So it seems that there is some problem when I use the library in my MAUI project.

Álvaro García
  • 18,114
  • 30
  • 102
  • 193
  • 1
    Do you have some more details about the platform? What kind of application is this? – davidfowl Aug 14 '22 at 15:32
  • It is just a class library in .NET 6.0. i which I create a class and in the constructor of the class I try to create the handler and the channel for a gRPC client. – Álvaro García Aug 14 '22 at 17:17
  • Why not just initialize a new list? – Pieterjan Aug 14 '22 at 17:21
  • @Pieterjan It should be initialize when it is instantiated the handler. In fact, I have tried to create a WPF application with one button which target is .NET 6.0 and the collection is not null. I have added the code in the original post. Anyway I have tried to create a new instance of the collection and I can't, because it is readonly, which is normal. – Álvaro García Aug 14 '22 at 17:30
  • `handler.ClientCertificates = new List<> { }`? – Pieterjan Aug 14 '22 at 17:32
  • 1
    @Pieterjan ClientCertificates it is read only, so I can't instantiate a new collection. – Álvaro García Aug 14 '22 at 17:34
  • @davidfowl It seems that I use the library that has the method that creates the handler is called from a MAUI project, it fails (at least if I debug the project from my phone). If I use the same library with a WPF project it works. – Álvaro García Aug 14 '22 at 17:49
  • Right, a library isn't a project type, so it's failing in a MAUI project? What platform does it fail on? – davidfowl Aug 15 '22 at 03:04
  • @davidfowl Yes, if I use the library in the MAUI project it fails, if I use the library in the WPF project it works. – Álvaro García Aug 15 '22 at 07:29
  • Maui running on what? iOS, Android or Windows? – davidfowl Aug 15 '22 at 14:39
  • @davidfowl I am debuging MAUI in a android mobile. – Álvaro García Aug 15 '22 at 17:36

0 Answers0