Scenario: I have a client implemented in C# that shoud connect to a server using gRPC using SSL for an encrypted Connection. However, the certificate used by the server may or may not be self-signed.
In the docs, I have only seen that I can set up a channel credential either insecure (no SSL at all) or secure by using custom root certificates (or using the public root CAs which will not validate a self-signed cert), which effectively means I would have to make sure that I install the self-signed server certificate as root. Basically, how do I do that programmatically?
var channelCredentials = new SslCredentials(rootAsPem);
// FIXME: specify that channelCredentials can accept self-signed certificates or fetch certificates?
var channel = new Channel("myservice.example.com", channelCredentials);
var client = new Greeter.GreeterClient(channel);
What I would like to implement is to ask the user like "hey, the server that you configured uses a self-signed certificate, are you OK with that?" and if so, install the certificate as a root certificate in the PEM.
My main Questions now are:
var httpClientHandler = new HttpClientHandler();
// Return `true` to allow certificates that are untrusted/invalid
httpClientHandler.ServerCertificateCustomValidationCallback =
HttpClientHandler.DangerousAcceptAnyServerCertificateValidator;
var httpClient = new HttpClient(httpClientHandler);
var channel = GrpcChannel.ForAddress("https://localhost:5001",
new GrpcChannelOptions { HttpClient = httpClient });
var client = new Greet.GreeterClient(channel);
https://docs.microsoft.com/en-us/aspnet/core/grpc/troubleshoot?view=aspnetcore-3.0
I had a similar problem and finally found a solution to establish HTTPs connection between
They key to a solution was to first download the server certificate using a regular HttpClient
and a Get
on the the gRPC target server. Through the HttpClientHandler
with its ServerCertifacteCustomValidationCallback
, you are able to get the X509 Certificate of the server and check if it is a self-signed certificate to prompt the user for confirmation of a probably unsafe connection. If it is confirmend, you can export the certificate to a PEM formatted string and than use it in the constructor of SslCredentials
. There is another important thing: the certificate has to contain the DNS name or the IP of the URL through which you are contacting the server, because the client performs a check on this. You can see a detailed error by enabling gRPC client debugging:
See my more detailed answer here:
https://stackoverflow.com/a/63565090/378415
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With