Skip to content

Commit d91dbb0

Browse files
Merge pull request #273951 from apwestgarth/apw_wincont_certs
Windows Container Notes Updated for certificate usage
2 parents 483f1ac + 44c3128 commit d91dbb0

File tree

1 file changed

+60
-2
lines changed

1 file changed

+60
-2
lines changed

articles/app-service/configure-ssl-certificate-in-code.md

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Use a TLS/SSL certificate in code
33
description: Learn how to use client certificates in your code. Authenticate with remote resources with a client certificate, or run cryptographic tasks with them.
44
ms.topic: article
55
ms.custom: linux-related-content
6-
ms.date: 02/15/2023
6+
ms.date: 05/01/2024
77
ms.reviewer: yutlin
88
ms.author: msangapu
99
author: msangapu-msft
@@ -148,7 +148,9 @@ The certificate file names are the certificate thumbprints.
148148
> App Service inject the certificate paths into Windows containers as the following environment variables `WEBSITE_PRIVATE_CERTS_PATH`, `WEBSITE_INTERMEDIATE_CERTS_PATH`, `WEBSITE_PUBLIC_CERTS_PATH`, and `WEBSITE_ROOT_CERTS_PATH`. It's better to reference the certificate path with the environment variables instead of hardcoding the certificate path, in case the certificate paths change in the future.
149149
>
150150
151-
In addition, [Windows Server Core containers](configure-custom-container.md#supported-parent-images) load the certificates into the certificate store automatically, in **LocalMachine\My**. To load the certificates, follow the same pattern as [Load certificate in Windows apps](#load-certificate-in-windows-apps). For Windows Nano based containers, use these file paths [Load the certificate directly from file](#load-certificate-from-file).
151+
In addition, [Windows Server Core and Windows Nano Server containers](configure-custom-container.md#supported-parent-images) load the certificates into the certificate store automatically, in **LocalMachine\My**. To load the certificates, follow the same pattern as [Load certificate in Windows apps](#load-certificate-in-windows-apps). For Windows Nano based containers, use these file paths [Load the certificate directly from file](#load-certificate-from-file).
152+
153+
### [Linux](#tab/linux)
152154

153155
The following C# code shows how to load a public certificate in a Linux app.
154156

@@ -177,6 +179,62 @@ var cert = new X509Certificate2(bytes);
177179
// Use the loaded certificate
178180
```
179181

182+
### [Windows](#tab/windows)
183+
184+
The following C# example shows how to load a public certificate in a .NET Framework app in a Windows Server Core Container.
185+
186+
```csharp
187+
using System;
188+
using System.Linq;
189+
using System.Security.Cryptography.X509Certificates;
190+
191+
string certThumbprint = "E661583E8FABEF4C0BEF694CBC41C28FB81CD870";
192+
bool validOnly = false;
193+
194+
using (X509Store certStore = new X509Store(StoreName.My, StoreLocation.LocalMachine))
195+
{
196+
certStore.Open(OpenFlags.ReadOnly);
197+
198+
X509Certificate2Collection certCollection = certStore.Certificates.Find(
199+
X509FindType.FindByThumbprint,
200+
// Replace below with your certificate's thumbprint
201+
certThumbprint,
202+
validOnly);
203+
// Get the first cert with the thumbprint
204+
X509Certificate2 cert = certCollection.OfType<X509Certificate2>().FirstOrDefault();
205+
206+
if (cert is null)
207+
throw new Exception($"Certificate with thumbprint {certThumbprint} was not found");
208+
209+
// Use certificate
210+
Console.WriteLine(cert.FriendlyName);
211+
212+
// Consider to call Dispose() on the certificate after it's being used, available in .NET 4.6 and later
213+
}
214+
```
215+
216+
The following C# example shows how to load a public certificate in a .NET Core app in a Windows Server Core or Windows Nano Server Container.
217+
218+
```csharp
219+
using System.Security.Cryptography.X509Certificates;
220+
221+
string Thumbprint = "C0CF730E216F5D690D1834446554DF5DC577A78B";
222+
223+
using X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
224+
{
225+
store.Open(OpenFlags.ReadOnly);
226+
227+
// Get the first cert with the thumbprint
228+
var certificate = store.Certificates.OfType<X509Certificate2>()
229+
.First(c => c.Thumbprint == Thumbprint) ?? throw new Exception($"Certificate with thumbprint {Thumbprint} was not found");
230+
231+
// Use certificate
232+
ViewData["certificateDetails"] = certificate.IssuerName.Name.ToString();
233+
}
234+
```
235+
236+
---
237+
180238
To see how to load a TLS/SSL certificate from a file in Node.js, PHP, Python, or Java, see the documentation for the respective language or web platform.
181239

182240
## When updating (renewing) a certificate

0 commit comments

Comments
 (0)