Skip to content

Commit 4087024

Browse files
authored
Add troubleshooting docs on untrusted cert with gRPC client (#14010)
1 parent c566f9f commit 4087024

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

aspnetcore/grpc/troubleshoot.md

+28-2
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ description: Troubleshoot errors when using gRPC on .NET Core.
55
monikerRange: '>= aspnetcore-3.0'
66
ms.author: jamesnk
77
ms.custom: mvc
8-
ms.date: 08/17/2019
8+
ms.date: 08/26/2019
99
uid: grpc/troubleshoot
1010
---
1111
# Troubleshoot gRPC on .NET Core
1212

1313
By [James Newton-King](https://twitter.com/jamesnk)
1414

15+
This document discusses commonly encountered problems when developing gRPC apps on .NET.
16+
1517
## Mismatch between client and service SSL/TLS configuration
1618

1719
The gRPC template and samples use [Transport Layer Security (TLS)](https://tools.ietf.org/html/rfc5246) to secure gRPC services by default. gRPC clients need to use a secure connection to call secured gRPC services successfully.
@@ -41,6 +43,30 @@ static async Task Main(string[] args)
4143

4244
All gRPC client implementations support TLS. gRPC clients from other languages typically require the channel configured with `SslCredentials`. `SslCredentials` specifies the certificate that the client will use, and it must be used instead of insecure credentials. For examples of configuring the different gRPC client implementations to use TLS, see [gRPC Authentication](https://www.grpc.io/docs/guides/auth/).
4345

46+
## Call a gRPC service with an untrusted/invalid certificate
47+
48+
The .NET gRPC client requires the service to have a trusted certificate. The following error message is returned when calling a gRPC service without a trusted certificate:
49+
50+
> Unhandled exception. System.Net.Http.HttpRequestException: The SSL connection could not be established, see inner exception.
51+
> ---> System.Security.Authentication.AuthenticationException: The remote certificate is invalid according to the validation procedure.
52+
53+
You may see this error if you are testing your app locally and the ASP.NET Core HTTPS development certificate is not trusted. For instructions to fix this issue, see [Trust the ASP.NET Core HTTPS development certificate on Windows and macOS](xref:security/enforcing-ssl#trust-the-aspnet-core-https-development-certificate-on-windows-and-macos).
54+
55+
If you are calling a gRPC service on another machine and are unable to trust the certificate then the gRPC client can be configured to ignore the invalid certificate. The following code uses [HttpClientHandler.ServerCertificateCustomValidationCallback](/dotnet/api/system.net.http.httpclienthandler.servercertificatecustomvalidationcallback) to allow calls without a trusted certificate:
56+
57+
```csharp
58+
var httpClientHandler = new HttpClientHandler();
59+
// Return `true` to allow certificates that are untrusted/invalid
60+
httpClientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => true;
61+
62+
var httpClient = new HttpClient(httpClientHandler);
63+
httpClient.BaseAddress = new Uri("https://localhost:5001");
64+
var client = GrpcClient.Create<Greeter.GreeterClient>(httpClient);
65+
```
66+
67+
> [!WARNING]
68+
> Untrusted certificates should only be used during app development. Production apps should always use valid certificates.
69+
4470
## Call insecure gRPC services with .NET Core client
4571

4672
Additional configuration is required to call insecure gRPC services with the .NET Core client. The gRPC client must set the `System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport` switch to `true` and use `http` in the server address:
@@ -50,7 +76,7 @@ Additional configuration is required to call insecure gRPC services with the .NE
5076
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
5177

5278
var httpClient = new HttpClient();
53-
// The port number(5000) must match the port of the gRPC server.
79+
// The address starts with "http://"
5480
httpClient.BaseAddress = new Uri("http://localhost:5000");
5581
var client = GrpcClient.Create<Greeter.GreeterClient>(httpClient);
5682
```

aspnetcore/tutorials/grpc/grpc-start.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ author: juntaoluo
44
description: This tutorial shows how to create a gRPC Service and gRPC client on ASP.NET Core. Learn how to create a gRPC Service project, edit a proto file, and add a duplex streaming call.
55
monikerRange: '>= aspnetcore-3.0'
66
ms.author: johluo
7-
ms.date: 8/23/2019
7+
ms.date: 8/26/2019
88
uid: tutorials/grpc/grpc-start
99
---
1010
# Tutorial: Create a gRPC client and server in ASP.NET Core
@@ -306,6 +306,9 @@ info: Microsoft.AspNetCore.Hosting.Diagnostics[2]
306306
Request finished in 78.32260000000001ms 200 application/grpc
307307
```
308308

309+
> [!NOTE]
310+
> The code in this article requires the ASP.NET Core HTTPS development certificate to secure the gRPC service. If the client fails with the message `The remote certificate is invalid according to the validation procedure.`, the development certificate is not trusted. For instructions to fix this issue, see [Trust the ASP.NET Core HTTPS development certificate on Windows and macOS](xref:security/enforcing-ssl#trust-the-aspnet-core-https-development-certificate-on-windows-and-macos).
311+
309312
### Next steps
310313

311314
* <xref:grpc/index>

0 commit comments

Comments
 (0)