|
1 | 1 | ---
|
2 |
| -title: Reload secrets and certificates automatically |
| 2 | +title: Reload Secrets and Certificates Automatically |
3 | 3 | titleSuffix: Azure App Configuration
|
4 |
| -description: Learn how to set up your application to automatically reload secrets and certificates from Key Vault. |
| 4 | +description: Find out how to use Azure App Configuration in your ASP.NET Core app to automatically reload secrets and certificates from Azure Key Vault. |
5 | 5 | services: azure-app-configuration
|
6 | 6 | author: avanigupta
|
7 | 7 | ms.service: azure-app-configuration
|
8 | 8 | ms.devlang: csharp
|
9 | 9 | ms.topic: how-to
|
10 |
| -ms.date: 05/25/2021 |
| 10 | +ms.date: 07/17/2025 |
11 | 11 | ms.author: avgupta
|
12 | 12 |
|
13 |
| - |
14 |
| -#Customer intent: I want my app to reload secrets or certificates from Key Vault without restarting my app. |
| 13 | +# customer intent: As a developer, I want to use Azure App Configuration in my ASP.NET Core app to automatically reload Azure Key Vault secrets and certificates so that I don't have to restart my app to get the latest values from Key Vault. |
15 | 14 | ---
|
16 | 15 |
|
17 | 16 | # Reload secrets and certificates from Key Vault automatically
|
18 | 17 |
|
19 |
| -App Configuration and Key Vault are complementary services used side by side in many applications. App Configuration helps you use the services together by creating keys in your App Configuration store that reference secrets or certificates stored in Key Vault. Since Key Vault stores the public and private key pair of a certificate as a secret, your application can retrieve any certificate as a secret from Key Vault. |
| 18 | +Azure App Configuration and Azure Key Vault are complementary services used side by side in many applications. App Configuration helps you use the services together by creating keys in your App Configuration store that reference secrets or certificates stored in Key Vault. Because Key Vault stores the public and private key pair of a certificate as a secret, your application can retrieve any certificate as a secret from Key Vault. |
20 | 19 |
|
21 |
| -As a good security practice, [secrets](/azure/key-vault/secrets/tutorial-rotation) and [certificates](/azure/key-vault/certificates/tutorial-rotate-certificates) should be rotated periodically. Once they have been rotated in Key Vault, you would want your application to pick up the latest secret and certificate values. There are two ways to achieve this without restarting your application: |
22 |
| -- Update a sentinel key-value to trigger the refresh of your entire configuration, thereby reloading all Key Vault secrets and certificates. For more information, see how to [use dynamic configuration in an ASP.NET Core app](./enable-dynamic-configuration-aspnet-core.md). |
23 |
| -- Periodically reload some or all secrets and certificates from Key Vault. |
| 20 | +As a good security practice, [secrets](/azure/key-vault/secrets/tutorial-rotation) and [certificates](/azure/key-vault/certificates/tutorial-rotate-certificates) should be rotated periodically. After they're rotated in Key Vault, your application should pick up the latest secret and certificate values. There are two ways to load these values without restarting your application: |
24 | 21 |
|
25 |
| -In the first option, you will have to update the sentinel key-value in App Configuration whenever you rotate secrets and certificates in Key Vault. This approach works well when you want to force an immediate reload of secrets and certificates in your application. However, when secrets and certificates are rotated automatically in Key Vault, your application may experience errors if you don't update the sentinel key-value in time. The second option allows you to completely automate this process. You can configure your application to reload secrets and certificates from Key Vault within your acceptable delay from the time of rotation. This tutorial will walk you through the second option. |
| 22 | +- Update the value of a sentinel key to trigger the refresh of your entire configuration. This process reloads all Key Vault secrets and certificates. For more information, see [Monitoring a sentinel key](howto-best-practices.md#monitoring-a-sentinel-key) and [Azure App Configuration dynamic settings sample](/samples/dotnet/samples/azure-app-config-dynamic-settings/). |
| 23 | +- Periodically reload some or all secrets and certificates from Key Vault. |
26 | 24 |
|
27 |
| -## Prerequisites |
| 25 | +When you use the first option, you need to update the value of the sentinel key in App Configuration whenever you rotate secrets and certificates in Key Vault. This approach works well when you want to force an immediate reload of secrets and certificates in your application. However, when secrets and certificates are rotated automatically in Key Vault, your application can experience errors if you don't update the value of the sentinel key in time. |
28 | 26 |
|
29 |
| -- This tutorial shows you how to set up your application to automatically reload secrets and certificates from Key Vault. It builds on the tutorial for implementing Key Vault references in your code. Before you continue, finish [Tutorial: Use Key Vault references in an ASP.NET Core app](./use-key-vault-references-dotnet-core.md) first. |
| 27 | +The second option provides a way to completely automate this process. You can configure your application to reload secrets and certificates from Key Vault within your acceptable delay from the time of rotation. This article walks you through the second option. |
30 | 28 |
|
31 |
| -- [Microsoft.Azure.AppConfiguration.AspNetCore](https://www.nuget.org/packages/Microsoft.Azure.AppConfiguration.AspNetCore) package v4.4.0 or later. |
| 29 | +## Prerequisites |
32 | 30 |
|
| 31 | +- The ASP.NET Core web app that you update when you complete the steps in [Tutorial: Use Key Vault references in an ASP.NET Core app](./use-key-vault-references-dotnet-core.md). This article shows you how to set up your application to automatically reload secrets and certificates from Key Vault. It builds on the tutorial for implementing Key Vault references in your code. |
| 32 | +- The key vault that you create when you complete the steps in [Tutorial: Use Key Vault references in an ASP.NET Core app](./use-key-vault-references-dotnet-core.md). |
| 33 | +- The [Microsoft.Azure.AppConfiguration.AspNetCore](https://www.nuget.org/packages/Microsoft.Azure.AppConfiguration.AspNetCore) package, version 4.4.0 or later. |
33 | 34 |
|
34 |
| -## Add an auto-rotating certificate to Key Vault |
| 35 | +## Add an automatically rotating certificate to Key Vault |
35 | 36 |
|
36 |
| - Follow the [Tutorial: Configure certificate auto-rotation in Key Vault](/azure/key-vault/certificates/tutorial-rotate-certificates) to add an auto-rotating certificate called **ExampleCertificate** to the Key Vault created in the previous tutorial. |
| 37 | +To add an automatically rotating certificate to a key vault, follow the steps in [Tutorial: Configure certificate autorotation in Key Vault](/azure/key-vault/certificates/tutorial-rotate-certificates). |
37 | 38 |
|
| 39 | +- Use the key vault that you create in [Tutorial: Use Key Vault references in an ASP.NET Core app](./use-key-vault-references-dotnet-core.md). |
| 40 | +- Name the certificate **ExampleCertificate**. |
38 | 41 |
|
39 | 42 | ## Add a reference to the Key Vault certificate in App Configuration
|
40 | 43 |
|
41 |
| -1. In the Azure portal, select **All resources**, and then select the App Configuration store instance that you created in the previous tutorial. |
42 |
| - |
43 |
| -1. Select **Configuration Explorer**. |
| 44 | +1. Go to the [Azure portal](https://portal.azure.com), select **All resources**, and then select the App Configuration instance that you use in [Tutorial: Use Key Vault references in an ASP.NET Core app](./use-key-vault-references-dotnet-core.md). |
44 | 45 |
|
45 |
| -1. Select **+ Create** > **Key vault reference**, and then specify the following values: |
46 |
| - - **Key**: Select **TestApp:Settings:KeyVaultCertificate**. |
47 |
| - - **Label**: Leave this value blank. |
48 |
| - - **Subscription**, **Resource group**, and **Key vault**: Enter the values corresponding to the Key Vault you created in the previous tutorial. |
49 |
| - - **Secret**: Select the secret named **ExampleCertificate** that you created in the previous section. |
50 |
| - - **Secret Version**: **Latest version**. |
| 46 | +1. Select **Configuration explorer**. |
51 | 47 |
|
52 |
| -> [!Note] |
53 |
| -> If you reference a specific version, reloading the secret or certificate from Key Vault will always return the same value. |
| 48 | +1. Select **Create** > **Key Vault reference**, and then enter the following values: |
| 49 | + - For **Key**: Enter **TestApp:Settings:KeyVaultCertificate**. |
| 50 | + - For **Label**: Leave the value blank. |
| 51 | + - For **Subscription**, **Resource group**, and **Key vault**: Enter the values you use when you create the key vault in [Tutorial: Use Key Vault references in an ASP.NET Core app](./use-key-vault-references-dotnet-core.md). |
| 52 | + - For **Secret**: Select the secret named **ExampleCertificate** that you create in the previous section. |
| 53 | + - For **Secret Version**: Select **Latest version**. |
54 | 54 |
|
| 55 | +> [!NOTE] |
| 56 | +> If you reference a specific version, reloading the secret or certificate from Key Vault always returns the same value. |
55 | 57 |
|
56 | 58 | ## Update code to reload Key Vault secrets and certificates
|
57 | 59 |
|
58 |
| -In your *Program.cs* file, update the `AddAzureAppConfiguration` method to set up a refresh interval for your Key Vault certificate using the `SetSecretRefreshInterval` method. With this change, your application will reload the public-private key pair for **ExampleCertificate** every 12 hours. |
| 60 | +Go to the folder that contains the ASP.NET Core web app project that you update in [Tutorial: Use Key Vault references in an ASP.NET Core app](./use-key-vault-references-dotnet-core.md). |
| 61 | + |
| 62 | +Open *Program.cs*, and replace the call to the `AddAzureAppConfiguration` method with the call in the following code. The updated call uses the `SetSecretRefreshInterval` method to set up a refresh interval for your Key Vault certificate. With this change, your application reloads the public-private key pair for **ExampleCertificate** every 12 hours. |
59 | 63 |
|
60 | 64 | ```csharp
|
61 |
| -config.AddAzureAppConfiguration(options => |
| 65 | +string endpoint = builder.Configuration.GetValue<string>("Endpoints:AppConfiguration"); |
| 66 | + |
| 67 | +builder.Configuration.AddAzureAppConfiguration(options => |
62 | 68 | {
|
63 |
| - options.Connect(settings["ConnectionStrings:AppConfig"]) |
64 |
| - .ConfigureKeyVault(kv => |
65 |
| - { |
66 |
| - kv.SetCredential(new DefaultAzureCredential()); |
67 |
| - kv.SetSecretRefreshInterval("TestApp:Settings:KeyVaultCertificate", TimeSpan.FromHours(12)); |
68 |
| - }); |
| 69 | + options.Connect(new Uri(endpoint), new DefaultAzureCredential()); |
| 70 | + |
| 71 | + options.ConfigureKeyVault(keyVaultOptions => |
| 72 | + { |
| 73 | + keyVaultOptions.SetCredential(new DefaultAzureCredential()); |
| 74 | + keyVaultOptions.SetSecretRefreshInterval("TestApp:Settings:KeyVaultCertificate", TimeSpan.FromHours(12)); |
| 75 | + }); |
69 | 76 | });
|
70 | 77 | ```
|
71 | 78 |
|
72 |
| -The first argument in `SetSecretRefreshInterval` method is the key of the Key Vault reference in App Configuration. This argument is optional. If the key parameter is omitted, the refresh interval will apply to all those secrets and certificates which do not have individual refresh intervals. |
| 79 | +The first argument in the `SetSecretRefreshInterval` method is the key of the Key Vault reference in App Configuration. This argument is optional. If you omit it, the specified refresh interval is applied to all secrets and certificates that don't have individual refresh intervals. |
73 | 80 |
|
74 |
| -Refresh interval defines the frequency at which your secrets and certificates will be reloaded from Key Vault, regardless of any changes to their values in Key Vault or App Configuration. If you want to reload secrets and certificates when their value changes in App Configuration, you can monitor them using the `ConfigureRefresh` method. For more information, see how to [use dynamic configuration in an ASP.NET Core app](./enable-dynamic-configuration-aspnet-core.md). |
75 |
| - |
76 |
| -Choose the refresh interval according to your acceptable delay after your secrets and certificates have been updated in Key Vault. It's also important to consider the [Key Vault service limits](/azure/key-vault/general/service-limits) to avoid being throttled. |
| 81 | +The second argument is the refresh interval. Its value specifies the frequency at which to reload your secrets and certificates from Key Vault, regardless of any changes to their values in Key Vault or App Configuration. If you want to reload secrets and certificates when their values change in App Configuration, you can use the `ConfigureRefresh` method to monitor them. For more information, see [Use dynamic configuration in an ASP.NET Core app](./enable-dynamic-configuration-aspnet-core.md). |
77 | 82 |
|
| 83 | +Choose the refresh interval according to your acceptable delay after your secrets and certificates are updated in Key Vault. It's also important to consider the [Key Vault service limits](/azure/key-vault/general/service-limits) to avoid throttling. |
78 | 84 |
|
79 | 85 | ## Clean up resources
|
80 | 86 |
|
81 | 87 | [!INCLUDE [azure-app-configuration-cleanup](../../includes/azure-app-configuration-cleanup.md)]
|
82 | 88 |
|
83 |
| - |
84 |
| -## Next steps |
85 |
| - |
86 |
| -In this tutorial, you learned how to set up your application to automatically reload secrets and certificates from Key Vault. To learn how to use Managed Identity to streamline access to App Configuration and Key Vault, continue to the next tutorial. |
| 89 | +## Next step |
87 | 90 |
|
88 | 91 | > [!div class="nextstepaction"]
|
89 |
| -> [Managed identity integration](./howto-integrate-azure-managed-service-identity.md) |
| 92 | +> [Use managed identities to streamline access to App Configuration and Key Vault](./howto-integrate-azure-managed-service-identity.md) |
0 commit comments