Skip to content

Commit 9c329be

Browse files
authored
Merge pull request #94594 from cephalin/postignite
add Linux support
2 parents a09d13f + 410f346 commit 9c329be

File tree

2 files changed

+68
-24
lines changed

2 files changed

+68
-24
lines changed

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

Lines changed: 66 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: Use SSL certificate in application code - Azure App Service | Microsoft Docs
2+
title: Use SSL certificate in code - Azure App Service | Microsoft Docs
33
description: Learn how to use client certificates to connect to remote resources that require them.
44
services: app-service
55
documentationcenter:
@@ -11,18 +11,18 @@ ms.service: app-service
1111
ms.workload: web
1212
ms.tgt_pltfrm: na
1313
ms.topic: article
14-
ms.date: 10/16/2019
14+
ms.date: 11/04/2019
1515
ms.author: cephalin
1616
ms.reviewer: yutlin
1717
ms.custom: seodec18
1818

1919
---
2020

21-
# Use an SSL certificate in your application code in Azure App Service
21+
# Use an SSL certificate in your code in Azure App Service
2222

23-
Your App Service app code may act as a client and access an external service that requires certificate authentication. This how-to guide shows how to use public or private certificates in your application code.
23+
In your application code, you can access the [public or private certificates you add to App Service](configure-ssl-certificate.md). Your app code may act as a client and access an external service that requires certificate authentication, or it may need to perform cryptographic tasks. This how-to guide shows how to use public or private certificates in your application code.
2424

25-
This approach to using certificates in your code makes use of the SSL functionality in App Service, which requires your app to be in **Basic** tier or above. Alternatively, you can [include the certificate file in your app repository](#load-certificate-from-file), but it's not a recommended practice for private certificates.
25+
This approach to using certificates in your code makes use of the SSL functionality in App Service, which requires your app to be in **Basic** tier or above. If your app is in **Free** or **Shared** tier, you can [include the certificate file in your app repository](#load-certificate-from-file).
2626

2727
When you let App Service manage your SSL certificates, you can maintain the certificates and your application code separately and safeguard your sensitive data.
2828

@@ -43,25 +43,24 @@ Find the certificate you want to use and copy the thumbprint.
4343

4444
![Copy the certificate thumbprint](./media/configure-ssl-certificate/create-free-cert-finished.png)
4545

46-
## Load the certificate
46+
## Make the certificate accessible
4747

48-
To use a certificate in your app code, add its thumbprint to the `WEBSITE_LOAD_CERTIFICATES` app setting, by running the following command in the <a target="_blank" href="https://shell.azure.com" >Cloud Shell</a>:
48+
To access a certificate in your app code, add its thumbprint to the `WEBSITE_LOAD_CERTIFICATES` app setting, by running the following command in the <a target="_blank" href="https://shell.azure.com" >Cloud Shell</a>:
4949

5050
```azurecli-interactive
5151
az webapp config appsettings set --name <app-name> --resource-group <resource-group-name> --settings WEBSITE_LOAD_CERTIFICATES=<comma-separated-certificate-thumbprints>
5252
```
5353

5454
To make all your certificates accessible, set the value to `*`.
5555

56-
> [!NOTE]
57-
> This setting places the specified certificates in the [Current User\My](/windows-hardware/drivers/install/local-machine-and-current-user-certificate-stores) store for most pricing tiers, but in the **Isolated** tier (i.e. app runs in an [App Service Environment](environment/intro.md)), it places the certificates in the [Local Machine\My](/windows-hardware/drivers/install/local-machine-and-current-user-certificate-stores) store.
58-
>
56+
## Load certificate in Windows apps
5957

60-
The configured certificates are now ready to be used by your code.
58+
The `WEBSITE_LOAD_CERTIFICATES` app setting makes the specified certificates accessible to your Windows hosted app in the Windows certificate store, and the location depends on the [pricing tier](overview-hosting-plans.md):
6159

62-
## Load the certificate in code
60+
- **Isolated** tier - in [Local Machine\My](/windows-hardware/drivers/install/local-machine-and-current-user-certificate-stores).
61+
- All other tiers - in [Current User\My](/windows-hardware/drivers/install/local-machine-and-current-user-certificate-stores).
6362

64-
Once your certificate is accessible, you access it in C# code by the certificate thumbprint. The following code loads a certificate with the thumbprint `E661583E8FABEF4C0BEF694CBC41C28FB81CD870`.
63+
In C# code, you access the certificate by the certificate thumbprint. The following code loads a certificate with the thumbprint `E661583E8FABEF4C0BEF694CBC41C28FB81CD870`.
6564

6665
```csharp
6766
using System;
@@ -86,31 +85,75 @@ certStore.Close();
8685
...
8786
```
8887

89-
<a name="file"></a>
90-
## Load certificate from file
88+
In Java code, you access the certificate from the "Windows-MY" store using the Subject Common Name field (see [Public key certificate](https://en.wikipedia.org/wiki/Public_key_certificate)). The following code shows how to load a private key certificate:
9189

92-
If you need to load a certificate file from your application directory, it's better to upload it using [FTPS](deploy-ftp.md) instead of [Git](deploy-local-git.md), for example. You should keep sensitive data like a private certificate out of source control.
90+
```java
91+
import org.springframework.web.bind.annotation.RestController;
92+
import org.springframework.web.bind.annotation.RequestMapping;
93+
import java.security.KeyStore;
94+
import java.security.cert.Certificate;
95+
import java.security.PrivateKey;
9396

94-
Even though you're loading the file directly in your .NET code, the library still verifies if the current user profile is loaded. To load the current user profile, set the `WEBSITE_LOAD_USER_PROFILE` app setting with the following command in the <a target="_blank" href="https://shell.azure.com" >Cloud Shell</a>.
97+
...
98+
KeyStore ks = KeyStore.getInstance("Windows-MY");
99+
ks.load(null, null);
100+
Certificate cert = ks.getCertificate("<subject-cn>");
101+
PrivateKey privKey = (PrivateKey) ks.getKey("<subject-cn>", ("<password>").toCharArray());
95102

96-
```azurecli-interactive
97-
az webapp config appsettings set --name <app-name> --resource-group <resource-group-name> --settings WEBSITE_LOAD_USER_PROFILE=1
103+
// Use the certificate and key
104+
...
98105
```
99106

100-
Once this setting is set, the following C# example loads a certificate called `mycert.pfx` from the `certs` directory of your app's repository.
107+
For languages that don't support or offer insufficient support for the Windows certificate store, see [Load certificate from file](#load-certificate-from-file).
108+
109+
## Load certificate in Linux apps
110+
111+
The `WEBSITE_LOAD_CERTIFICATES` app settings makes the specified certificates accessible to your Linux hosted apps (including custom container apps) as files. The files are found under the following directories:
112+
113+
- Private certificates - `/var/ssl/private` ( `.p12` files)
114+
- Public certificates - `/var/ssl/certs` ( `.der` files)
115+
116+
The certificate file names are the certificate thumbprints. The following C# code shows how to load a public certificate in a Linux app.
101117

102118
```csharp
103119
using System;
104120
using System.Security.Cryptography.X509Certificates;
105121

106122
...
107-
// Replace the parameter with "~/<relative-path-to-cert-file>".
108-
string certPath = Server.MapPath("~/certs/mycert.pfx");
123+
var bytes = System.IO.File.ReadAllBytes("/var/ssl/certs/<thumbprint>.der");
124+
var cert = new X509Certificate2(bytes);
125+
126+
// Use the loaded certificate
127+
```
128+
129+
To see how to load an SSL certificate from a file in Node.js, PHP, Python, Java, or Ruby, see the documentation for the respective language or web platform.
130+
131+
## Load certificate from file
132+
133+
If you need to load a certificate file that you upload manually, it's better to upload the certificate using [FTPS](deploy-ftp.md) instead of [Git](deploy-local-git.md), for example. You should keep sensitive data like a private certificate out of source control.
134+
135+
> [!NOTE]
136+
> ASP.NET and ASP.NET Core on Windows must access the certificate store even if you load a certificate from a file. To load a certificate file in a Windows .NET app, load the current user profile with the following command in the <a target="_blank" href="https://shell.azure.com" >Cloud Shell</a>:
137+
>
138+
> ```azurecli-interactive
139+
> az webapp config appsettings set --name <app-name> --resource-group <resource-group-name> --settings WEBSITE_LOAD_USER_PROFILE=1
140+
> ```
141+
142+
The following C# example loads a public certificate from a relative path in your app:
143+
144+
```csharp
145+
using System;
146+
using System.Security.Cryptography.X509Certificates;
109147
110-
X509Certificate2 cert = GetCertificate(certPath, signatureBlob.Thumbprint);
111148
...
149+
var bytes = System.IO.File.ReadAllBytes("~/<relative-path-to-cert-file>");
150+
var cert = new X509Certificate2(bytes);
151+
152+
// Use the loaded certificate
112153
```
113154
155+
To see how to load an SSL certificate from a file in Node.js, PHP, Python, Java, or Ruby, see the documentation for the respective language or web platform.
156+
114157
## More resources
115158

116159
* [Secure a custom DNS name with an SSL binding](configure-ssl-bindings.md)

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ To secure a custom domain in an SSL binding, the certificate has additional requ
6060

6161
The free App Service Managed Certificate is a turn-key solution for securing your custom DNS name in App Service. It's a fully functional SSL certificate that's managed by App Service and renewed automatically. The free certificate comes with the following limitations:
6262

63+
- Does not support wildcard certificates.
6364
- Does not support naked domains.
6465
- Is not exportable.
6566

@@ -276,7 +277,7 @@ Click **Upload**.
276277

277278
![Upload public certificate in App Service](./media/configure-ssl-certificate/upload-public-cert.png)
278279

279-
Once the certificate is uploaded, copy the certificate thumbprint and see [Make the certificate accessible](configure-ssl-certificate-in-code.md#load-the-certificate).
280+
Once the certificate is uploaded, copy the certificate thumbprint and see [Make the certificate accessible](configure-ssl-certificate-in-code.md#make-the-certificate-accessible).
280281

281282
## Manage App Service certificates
282283

0 commit comments

Comments
 (0)