Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When I try to create a handler in the client for a gRPC service HttpClientHandler.ClientCertificates is null #73960

Closed
ComptonAlvaro opened this issue Aug 15, 2022 · 9 comments

Comments

@ComptonAlvaro
Copy link

Description

I open this issue because I opened in the maui project and they told that this is more a problem of dotnet.

this is the maui project: https://github.com/dotnet/maui

this is the issued that I opened in the maui project: dotnet/maui#9430

DESCRIPTION OF THE PROBLEM

I have added anyway a zip with a project that reproduces t he problem.

I have a solution with 3 project. All projects are targeted as .NET 6.0.

First one is a class library with one class, in which constructor I have this code:

    public MyLibraryClass()
    {
        HttpClientHandler miHttpHandler = new HttpClientHandler();
    }

The second project is a MAUI project.

In the click button of the main page I have this code:

private async void OnBtnPingClicked(object sender, EventArgs e)
{
	MyLibraryClass myClass = new MyLibraryClass();
}

The third project is a WPF project, in which in a click event of a button in the main window I have this code in the code-behind:

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        MyLibraryClass myClass = new MyLibraryClass();
    }

When I debug the WPF project and set a breakpoint at the end of the method, the prperty miHandler.ClientCertificates in the method in the library class is not null. However, if I debug the MAUI project in an android phone and debug, the property miHandler.ClientCertificates is null.

My objective it is to use a gRPC client that uses certificates and I could use in many clients, MAUI and WPF in this case. But if the ClientCertificates property is null, I can't set the certificates in the MAUI project.

Thanks.
HttpHandlerError.zip

Reproduction Steps

1.- Create a new solution.
2.- Create a class library with target is .NET 6.0.
3.- Create a class with a method with the code that create a new HttpCientHander.
4.- Create a MAUI project with target .NET 6.0.
5.- Create a button that instantiate an object of the class library.
6.- Debug the MAUI project and see the value of the property miHanlder.ClientCertificates. It should to be null (this is the error)
7.- Create a WPF application that targets .NET 6.0.
8.- Add a new button which code instantiate an onject of type of the class in the library.
9.- Debug the WPF project and check the value of miHandler.ClientCertificates property. It should be not null. This is correct.

Expected behavior

That handler.ClientCertificates wouldn't be null.

Actual behavior

That handler.ClientCertificates is null.

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

@dotnet-issue-labeler
Copy link

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Aug 15, 2022
@ghost
Copy link

ghost commented Aug 15, 2022

Tagging subscribers to this area: @dotnet/ncl, @vcsjones
See info in area-owners.md if you want to be subscribed.

Issue Details

Description

I open this issue because I opened in the maui project and they told that this is more a problem of dotnet.

this is the maui project: https://github.com/dotnet/maui

this is the issued that I opened in the maui project: dotnet/maui#9430

DESCRIPTION OF THE PROBLEM

I have added anyway a zip with a project that reproduces t he problem.

I have a solution with 3 project. All projects are targeted as .NET 6.0.

First one is a class library with one class, in which constructor I have this code:

    public MyLibraryClass()
    {
        HttpClientHandler miHttpHandler = new HttpClientHandler();
    }

The second project is a MAUI project.

In the click button of the main page I have this code:

private async void OnBtnPingClicked(object sender, EventArgs e)
{
	MyLibraryClass myClass = new MyLibraryClass();
}

The third project is a WPF project, in which in a click event of a button in the main window I have this code in the code-behind:

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        MyLibraryClass myClass = new MyLibraryClass();
    }

When I debug the WPF project and set a breakpoint at the end of the method, the prperty miHandler.ClientCertificates in the method in the library class is not null. However, if I debug the MAUI project in an android phone and debug, the property miHandler.ClientCertificates is null.

My objective it is to use a gRPC client that uses certificates and I could use in many clients, MAUI and WPF in this case. But if the ClientCertificates property is null, I can't set the certificates in the MAUI project.

Thanks.
HttpHandlerError.zip

Reproduction Steps

1.- Create a new solution.
2.- Create a class library with target is .NET 6.0.
3.- Create a class with a method with the code that create a new HttpCientHander.
4.- Create a MAUI project with target .NET 6.0.
5.- Create a button that instantiate an object of the class library.
6.- Debug the MAUI project and see the value of the property miHanlder.ClientCertificates. It should to be null (this is the error)
7.- Create a WPF application that targets .NET 6.0.
8.- Add a new button which code instantiate an onject of type of the class in the library.
9.- Debug the WPF project and check the value of miHandler.ClientCertificates property. It should be not null. This is correct.

Expected behavior

That handler.ClientCertificates wouldn't be null.

Actual behavior

That handler.ClientCertificates is null.

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

Author: ComptonAlvaro
Assignees: -
Labels:

area-System.Net.Security, untriaged

Milestone: -

@ghost
Copy link

ghost commented Aug 15, 2022

Tagging subscribers to 'arch-android': @steveisok, @akoeplinger
See info in area-owners.md if you want to be subscribed.

Issue Details

Description

I open this issue because I opened in the maui project and they told that this is more a problem of dotnet.

this is the maui project: https://github.com/dotnet/maui

this is the issued that I opened in the maui project: dotnet/maui#9430

DESCRIPTION OF THE PROBLEM

I have added anyway a zip with a project that reproduces t he problem.

I have a solution with 3 project. All projects are targeted as .NET 6.0.

First one is a class library with one class, in which constructor I have this code:

    public MyLibraryClass()
    {
        HttpClientHandler miHttpHandler = new HttpClientHandler();
    }

The second project is a MAUI project.

In the click button of the main page I have this code:

private async void OnBtnPingClicked(object sender, EventArgs e)
{
	MyLibraryClass myClass = new MyLibraryClass();
}

The third project is a WPF project, in which in a click event of a button in the main window I have this code in the code-behind:

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        MyLibraryClass myClass = new MyLibraryClass();
    }

When I debug the WPF project and set a breakpoint at the end of the method, the prperty miHandler.ClientCertificates in the method in the library class is not null. However, if I debug the MAUI project in an android phone and debug, the property miHandler.ClientCertificates is null.

My objective it is to use a gRPC client that uses certificates and I could use in many clients, MAUI and WPF in this case. But if the ClientCertificates property is null, I can't set the certificates in the MAUI project.

Thanks.
HttpHandlerError.zip

Reproduction Steps

1.- Create a new solution.
2.- Create a class library with target is .NET 6.0.
3.- Create a class with a method with the code that create a new HttpCientHander.
4.- Create a MAUI project with target .NET 6.0.
5.- Create a button that instantiate an object of the class library.
6.- Debug the MAUI project and see the value of the property miHanlder.ClientCertificates. It should to be null (this is the error)
7.- Create a WPF application that targets .NET 6.0.
8.- Add a new button which code instantiate an onject of type of the class in the library.
9.- Debug the WPF project and check the value of miHandler.ClientCertificates property. It should be not null. This is correct.

Expected behavior

That handler.ClientCertificates wouldn't be null.

Actual behavior

That handler.ClientCertificates is null.

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

Author: ComptonAlvaro
Assignees: -
Labels:

area-System.Net.Security, os-android, untriaged

Milestone: -

@akoeplinger
Copy link
Member

/cc @simonrozsival

@simonrozsival
Copy link
Member

Hi @ComptonAlvaro! The HttpClientHandler uses Xamarin's native AndroidMessageHandler under the hood by default and it doesn't seem to support the ClientCertificates property (even though it has that property).

What you should try is adding the following property to your csproj file:

<UseNativeHttpHandler>false</UseNativeHttpHandler>

This will force HttpClientHandler to use System.Net.Http.SocketsHttpHandler instead which should be suitable for your use case. Let me know if this solves your problem.

@ComptonAlvaro
Copy link
Author

@simonrozsival Thanks for your suggestion.

It works.

Now the question if it AndroidMessageHandler will support ClientCertificates to avoid to do this setup, because it would be more intiuitve, because when I use .NET 6 and it is said it can be used by MAUI, I expect that it will can use in other the projects that can reference this library.

Thanks.

@akoeplinger
Copy link
Member

akoeplinger commented Aug 16, 2022

One alternative is also to use HttpMessageHandler miHttpHandler = new SocketsHttpHandler(); and pass that to gRPC.

This will allow you to not set <UseNativeHttpHandler>false</UseNativeHttpHandler> in the .csproj so other parts of your app still use the native http handler (which is the recommended one for most scenarios).

@simonrozsival do you think we can implement ClientCertificates support in the Android native handler? Or should we work with gRPC folks to default to the Sockets handler on Android?

@akoeplinger akoeplinger removed the untriaged New issue has not been triaged by the area owner label Aug 16, 2022
@akoeplinger akoeplinger added this to the Future milestone Aug 16, 2022
@simonrozsival
Copy link
Member

simonrozsival commented Aug 16, 2022

@ComptonAlvaro I'm glad that helped. Thanks for the feedback, we should consider extending the functionality of the native handler in future .NET releases.

@akoeplinger The sockets handler is already the default HTTP handler in .NET 5+ on gRPC, see https://github.com/grpc/grpc-dotnet/blob/7ef184f3c4cd62fbc3cde55e4bb3e16b58258ca1/src/Shared/HttpHandlerFactory.cs#L33.

But in this case, @ComptonAlvaro needs to configure the handler in a specific way and so the handler must be created manually and then passed to the GrpcChannel. Implementing the support should be doable by wrapping ClientCertificates in a custom KeyStore, but I'm not sure how it fits in our .NET 8 plans. /cc @steveisok

@simonrozsival
Copy link
Member

The issue isn't specific to gRPC and it describes missing functionality in Xamarin.Android. I opened a new issue in the Xamarin Android repository and so I'm now closing this issue.

@karelz karelz modified the milestones: Future, 8.0.0 Aug 23, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Sep 22, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants