Skip to content

Commit c16301d

Browse files
stebetadegeoIEvangelist
authored
Update channel-credentials.md (#24051)
* Update channel-credentials.md Adding workaround for PEM loaded client certificates on Windows. * Fix markdown errors * Update channel-credentials.md Making it clear when the workaround should be applied. * Update docs/architecture/grpc-for-wcf-developers/channel-credentials.md Fixing after code review. Co-authored-by: Andy (Steve) De George <67293991+adegeo@users.noreply.github.com> * Update docs/architecture/grpc-for-wcf-developers/channel-credentials.md Co-authored-by: David Pine <david.pine@microsoft.com> * Update docs/architecture/grpc-for-wcf-developers/channel-credentials.md Co-authored-by: David Pine <david.pine@microsoft.com> * Update docs/architecture/grpc-for-wcf-developers/channel-credentials.md Co-authored-by: David Pine <david.pine@microsoft.com> * Intro paras; formatting. * Update channel-credentials.md Co-authored-by: Andy (Steve) De George <67293991+adegeo@users.noreply.github.com> Co-authored-by: David Pine <david.pine@microsoft.com>
1 parent 516ec8d commit c16301d

File tree

1 file changed

+48
-1
lines changed

1 file changed

+48
-1
lines changed

docs/architecture/grpc-for-wcf-developers/channel-credentials.md

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: Channel credentials - gRPC for WCF Developers
33
description: How to implement and use gRPC channel credentials in ASP.NET Core 3.0.
4-
ms.date: 12/15/2020
4+
ms.date: 06/28/2021
55
---
66

77
# Channel credentials
@@ -92,6 +92,10 @@ public class Startup
9292

9393
With the `Grpc.Net.Client` package, you configure certificates on an <xref:System.Net.Http.HttpClient> instance that is provided to the `GrpcChannel` used for the connection.
9494

95+
### Load a client certificate from a .PFX file
96+
97+
A certificate can be loaded from a _.pfx_ file.
98+
9599
```csharp
96100
class Program
97101
{
@@ -117,6 +121,49 @@ class Program
117121
}
118122
```
119123

124+
### Load a client certificate from certificate and private key .PEM files
125+
126+
A certificate can be loaded from a certificate and private key _.pem_ file.
127+
128+
```csharp
129+
class Program
130+
{
131+
static async Task Main(string[] args)
132+
{
133+
// Assume path to a certificate and private key .pem files are passed from command line
134+
string certificatePem = File.ReadAllText(args[0]);
135+
string privateKeyPem = File.ReadAllText(args[1]);
136+
var cert = X509Certificate2.CreateFromPem(certificatePem, privateKeyPem);
137+
138+
var handler = new HttpClientHandler();
139+
handler.ClientCertificates.Add(cert);
140+
using HttpClient httpClient = new(handler);
141+
142+
var channel = GrpcChannel.ForAddress("https://localhost:5001/", new GrpcChannelOptions
143+
{
144+
HttpClient = httpClient
145+
});
146+
147+
var grpc = new Greeter.GreeterClient(channel);
148+
var response = await grpc.SayHelloAsync(new HelloRequest { Name = "Bob" });
149+
System.Console.WriteLine(response.Message);
150+
}
151+
}
152+
```
153+
154+
> [!NOTE]
155+
> Due to an internal Windows bug as [documented here](https://github.com/dotnet/runtime/issues/23749#issuecomment-388231655), you'll need to apply the following a workaround if the certificate is created from certificate and private key PEM data.
156+
>
157+
> ```csharp
158+
> X509Certificate2 cert = X509Certificate2.CreateFromPem(certificatePem, rsaPrivateKeyPem);
159+
> if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
160+
> {
161+
> var originalCert = cert;
162+
> cert = new X509Certificate2(cert.Export(X509ContentType.Pkcs12));
163+
> originalCert.Dispose();
164+
> }
165+
> ```
166+
120167
## Combine ChannelCredentials and CallCredentials
121168
122169
You can configure your server to use both certificate and token authentication. To do this, apply the certificate changes to the Kestrel server, and use the JWT bearer middleware in ASP.NET Core.

0 commit comments

Comments
 (0)