Skip to content

Commit 2b7f93a

Browse files
authored
Merge pull request #91673 from BobbySchmidt2/use-key-vault-references-dotnet-core
edit pass: Use key vault references dotnet core
2 parents 754c30f + cb0aa84 commit 2b7f93a

File tree

1 file changed

+70
-54
lines changed

1 file changed

+70
-54
lines changed

articles/azure-app-configuration/use-key-vault-references-dotnet-core.md

Lines changed: 70 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -20,77 +20,84 @@ ms.custom: mvc
2020
---
2121
# Tutorial: Use Key Vault references in an ASP.NET Core app
2222

23-
In this tutorial, you'll learn how to use the Azure App Configuration service together with Azure Key Vault. These are complementary services, which will be used side by side in most application deployments. To help you use them together, App Configuration allows you to create keys that reference values stored in Key Vault. When you do this, App Configuration stores the URI to the Key Vault value, rather than the value itself. Your application retrieves the value of this key using the App Configuration client provider, just like any other key stored in App Configuration. The client provider recognizes it as a Key Vault reference, and calls out to Key Vault to retrieve the value. Your application is responsible for authenticating properly to both App Configuration and Key Vault. The two services don't communicate directly.
23+
In this tutorial, you learn how to use the Azure App Configuration service together with Azure Key Vault. App Configuration and Key Vault are complementary services used side by side in most application deployments.
2424

25-
This tutorial shows how you can implement Key Vault references in your code. It builds on the web app introduced in the quickstarts. Before you continue, finish [Create an ASP.NET Core app with App Configuration](./quickstart-aspnet-core-app.md) first.
25+
App Configuration helps you use the services together by creating keys that reference values stored in Key Vault. When App Configuration creates such keys, it stores the URIs of Key Vault values rather than the values themselves.
2626

27-
You can use any code editor to do the steps in this tutorial. [Visual Studio Code](https://code.visualstudio.com/) is an excellent option that's available on the Windows, macOS, and Linux platforms.
27+
Your application uses the App Configuration client provider to retrieve Key Vault references, just as it does for any other keys stored in App Configuration. In this case, the values stored in App Configuration are URIs that reference the values in the Key Vault. They are not Key Vault values or credentials. Because the client provider recognizes the keys as Key Vault references, it uses Key Vault to retrieve their values.
28+
29+
Your application is responsible for authenticating properly to both App Configuration and Key Vault. The two services don't communicate directly.
30+
31+
This tutorial shows you how to implement Key Vault references in your code. It builds on the web app introduced in the quickstarts. Before you continue, finish [Create an ASP.NET Core app with App Configuration](./quickstart-aspnet-core-app.md) first.
32+
33+
You can use any code editor to do the steps in this tutorial. For example, [Visual Studio Code](https://code.visualstudio.com/) is a cross-platform code editor that's available for the Windows, macOS, and Linux operating systems.
2834

2935
In this tutorial, you learn how to:
3036

3137
> [!div class="checklist"]
32-
> * Create an App Config key that references a value stored in Key Vault
33-
> * Access the value of this key from an ASP.NET Core web application
38+
> * Create an App Configuration key that references a value stored in Key Vault.
39+
> * Access the value of this key from an ASP.NET Core web application.
3440
3541
## Prerequisites
3642

37-
To do this tutorial, install the [.NET Core SDK](https://dotnet.microsoft.com/download).
43+
Before you start this tutorial, install the [.NET Core SDK](https://dotnet.microsoft.com/download).
3844

3945
[!INCLUDE [quickstarts-free-trial-note](../../includes/quickstarts-free-trial-note.md)]
4046

4147
## Create a vault
4248

43-
1. Select the **Create a resource** option on the upper left-hand corner of the Azure portal
49+
1. Select the **Create a resource** option in the upper-left corner of the Azure portal:
4450

45-
![Output after Key Vault creation completes](./media/quickstarts/search-services.png)
46-
2. In the Search box, enter **Key Vault**.
47-
3. From the results list, choose **Key Vault**.
48-
4. On the Key Vault section, choose **Create**.
49-
5. On the **Create key vault** section provide the following information:
50-
- **Name**: A unique name is required. For this quickstart, we use **Contoso-vault2**.
51-
- **Subscription**: Choose a subscription.
52-
- Under **Resource Group**, choose **Create new** and enter a resource group name.
53-
- In the **Location** pull-down menu, choose a location.
54-
- Leave the other options to their defaults.
55-
6. After providing the information above, select **Create**.
51+
![Output after key vault creation is complete](./media/quickstarts/search-services.png)
52+
1. In the search box, enter **Key Vault**.
53+
1. From the results list, select **Key vaults** on the left.
54+
1. In **Key vaults**, select **Add**.
55+
1. On the right in **Create key vault**, provide the following information:
56+
- Select **Subscription** to choose a subscription.
57+
- In **Resource Group**, select **Create new** and enter a resource group name.
58+
- In **Key vault name**, a unique name is required. For this tutorial, enter **Contoso-vault2**.
59+
- In the **Region** drop-down list, choose a location.
60+
1. Leave the other **Create key vault** options with their default values.
61+
1. Select **Create**.
5662

5763
At this point, your Azure account is the only one authorized to access this new vault.
5864

59-
![Output after Key Vault creation completes](./media/quickstarts/vault-properties.png)
65+
![Output after key vault creation is complete](./media/quickstarts/vault-properties.png)
6066

6167
## Add a secret to Key Vault
6268

63-
To add a secret to the vault, you just need to take a couple of additional steps. In this case, we add a message that we can use to test Key Vault retrieval. The message is called **Message** and we store the value of **Hello from Key Vault** in it.
69+
To add a secret to the vault, you need to take just a few additional steps. In this case, add a message that you can use to test Key Vault retrieval. The message is called **Message**, and you store the value "Hello from Key Vault" in it.
6470

65-
1. On the Key Vault properties pages, select **Secrets**.
66-
1. Click on **Generate/Import**.
67-
1. On the **Create a secret** screen choose the following values:
68-
- **Upload options**: Manual.
69-
- **Name**: Message
70-
- **Value**: Hello from Key Vault
71-
- Leave the other values to their defaults. Click **Create**.
71+
1. From the Key Vault properties pages, select **Secrets**.
72+
1. Select **Generate/Import**.
73+
1. In the **Create a secret** pane, enter the following values:
74+
- **Upload options**: Enter **Manual**.
75+
- **Name**: Enter **Message**.
76+
- **Value**: Enter **Hello from Key Vault**.
77+
1. Leave the other **Create a secret** properties with their default values.
78+
1. Select **Create**.
7279

73-
## Add a Key Vault reference to App Config
80+
## Add a Key Vault reference to App Configuration
7481

75-
1. Sign in to the [Azure portal](https://portal.azure.com). Select **All resources**, and select the app configuration store instance that you created in the quickstart.
82+
1. Sign in to the [Azure portal](https://portal.azure.com). Select **All resources**, and then select the App Configuration store instance that you created in the quickstart.
7683

77-
1. Click **Configuration Explorer**
84+
1. Select **Configuration Explorer**.
7885

79-
1. Click **+ Create** > **Key vault reference** and choose the following values:
80-
- **Key**: TestApp:Settings:KeyVaultMessage
81-
- **Label**: Leave blank
82-
- **Subscription**, **Resource group**, **Key vault**: Choose the options corresponding to the Key Vault that you created in the previous section.
83-
- **Secret**: Select the secret called **Message** that you created in the previous section.
86+
1. Select **+ Create** > **Key vault reference**, and then specify the following values:
87+
- **Key**: Select **TestApp:Settings:KeyVaultMessage**.
88+
- **Label**: Leave this value blank.
89+
- **Subscription**, **Resource group**, and **Key vault**: Enter the values corresponding to those in the key vault you created in the previous section.
90+
- **Secret**: Select the secret named **Message** that you created in the previous section.
8491

8592
## Connect to Key Vault
8693

87-
1. For this tutorial, you'll use a service principal for authentication to KeyVault. To create this service principal, use the Azure CLI [az ad sp create-for-rbac](/cli/azure/ad/sp?view=azure-cli-latest#az-ad-sp-create-for-rbac) command:
94+
1. In this tutorial, you use a service principal for authentication to Key Vault. To create this service principal, use the Azure CLI [az ad sp create-for-rbac](/cli/azure/ad/sp?view=azure-cli-latest#az-ad-sp-create-for-rbac) command:
8895

8996
```azurecli
9097
az ad sp create-for-rbac -n "http://mySP" --sdk-auth
9198
```
9299
93-
This operation will return a series of key / value pairs.
100+
This operation returns a series of key/value pairs:
94101
95102
```console
96103
{
@@ -107,28 +114,32 @@ To add a secret to the vault, you just need to take a couple of additional steps
107114
}
108115
```
109116
110-
1. Run the following command to allow the service principal to access your key vault:
117+
1. Run the following command to let the service principal access your key vault:
111118
112-
az keyvault set-policy -n <your-unique-keyvault-name> --spn <clientId-of-your-service-principal> --secret-permissions delete get list set --key-permissions create decrypt delete encrypt get list unwrapKey wrapKey
119+
```
120+
az keyvault set-policy -n <your-unique-keyvault-name> --spn <clientId-of-your-service-principal> --secret-permissions delete get list set --key-permissions create decrypt delete encrypt get list unwrapKey wrapKey
121+
```
113122
114-
1. Add secrets for *clientId* and *clientSecret* to Secrets Manager. These commands must be executed in the same directory as the *.csproj* file.
123+
1. In the following commands, add secrets in place of *clientId* and *clientSecret* to Secrets Manager. The commands must be run in the same directory as the *.csproj* file.
115124
116-
dotnet user-secrets set ConnectionStrings:KeyVaultClientId <clientId-of-your-service-principal>
117-
dotnet user-secrets set ConnectionStrings:KeyVaultClientSecret <clientSecret-of-your-service-principal>
125+
```
126+
dotnet user-secrets set ConnectionStrings:KeyVaultClientId <clientId-of-your-service-principal>
127+
dotnet user-secrets set ConnectionStrings:KeyVaultClientSecret <clientSecret-of-your-service-principal>
128+
```
118129
119130
> [!NOTE]
120131
> These Key Vault credentials are used only within your application. Your application authenticates directly to Key Vault with these credentials. They are never passed to the App Configuration service.
121132
122133
## Update your code to use a Key Vault reference
123134
124-
1. Open *Program.cs*, and add references to required packages.
135+
1. Open *Program.cs*, and add references to the following required packages:
125136
126137
```csharp
127138
using Microsoft.Azure.KeyVault;
128139
using Microsoft.IdentityModel.Clients.ActiveDirectory;
129140
```
130141
131-
1. Update the `CreateWebHostBuilder` method to use App Configuration by calling the `config.AddAzureAppConfiguration()` method. Include the `UseAzureKeyVault` option, passing in a new `KeyVaultClient` reference to your Key Vault.
142+
1. Update the `CreateWebHostBuilder` method to use App Configuration by calling the `config.AddAzureAppConfiguration` method. Include the `UseAzureKeyVault` option to pass in a new `KeyVaultClient` reference to your Key Vault.
132143
133144
```csharp
134145
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
@@ -151,7 +162,9 @@ To add a secret to the vault, you just need to take a couple of additional steps
151162
.UseStartup<Startup>();
152163
```
153164
154-
1. Once you've passed the *KeyVaultClient* reference to the `UseAzureKeyVault` method when initializing the connection to App Config, you can access the values of Key Vault references in the same way you access the values of regular App Config keys. To see this process in action, open *Index.cshtml* in the Views > Home directory. Replace its content with the following code:
165+
1. When you initialized the connection to App Configuration, you passed the `KeyVaultClient` reference to the `UseAzureKeyVault` method. After the initialization, you can access the values of Key Vault references in the same way you access the values of regular App Configuration keys.
166+
167+
To see this process in action, open *Index.cshtml* in the **Views** > **Home** folder. Replace its contents with the following code:
155168
156169
```html
157170
@using Microsoft.Extensions.Configuration
@@ -171,30 +184,33 @@ To add a secret to the vault, you just need to take a couple of additional steps
171184
and @Configuration["TestApp:Settings:KeyVaultMessage"]</h1>
172185
```
173186
174-
You access the value of the Key Vault reference *TestApp:Settings:KeyVaultMessage* in the same way as the configuration value *TestApp:Settings:Message*
187+
You access the value of the Key Vault reference **TestApp:Settings:KeyVaultMessage** in the same way as for the configuration value of **TestApp:Settings:Message**.
175188
176189
## Build and run the app locally
177190
178191
1. To build the app by using the .NET Core CLI, run the following command in the command shell:
179192
180-
dotnet build
181-
182-
2. After the build successfully completes, run the following command to run the web app locally:
193+
```
194+
dotnet build
195+
```
183196
184-
dotnet run
197+
1. After the build is complete, use the following command to run the web app locally:
185198
186-
3. Open a browser window, and go to `http://localhost:5000`, which is the default URL for the web app hosted locally.
199+
```
200+
dotnet run
201+
```
187202
188-
![Quickstart app launch local](./media/key-vault-reference-launch-local.png)
203+
1. Open a browser window, and go to `http://localhost:5000`, which is the default URL for the web app hosted locally.
189204
205+
![Quickstart local app launch](./media/key-vault-reference-launch-local.png)
190206
191207
## Clean up resources
192208
193209
[!INCLUDE [azure-app-configuration-cleanup](../../includes/azure-app-configuration-cleanup.md)]
194210
195211
## Next steps
196212
197-
In this tutorial, you added an Azure managed service identity to streamline access to App Configuration and improve credential management for your app. To learn more about how to use App Configuration, continue to the Azure CLI samples.
213+
In this tutorial, you created an App Configuration key that references a value stored in Key Vault. To learn how to add an Azure-managed service identity that streamlines access to App Configuration and Key Vault, continue to the next tutorial.
198214
199215
> [!div class="nextstepaction"]
200-
> [CLI samples](./cli-samples.md)
216+
> [Managed identity integration](./howto-integrate-azure-managed-service-identity.md)

0 commit comments

Comments
 (0)