Skip to content

Commit 039ebb4

Browse files
authored
Merge pull request #219702 from ghogen/main
Update Visual Studio Key Vault article for VS 2022
2 parents 70e2b72 + 99db1ac commit 039ebb4

File tree

3 files changed

+23
-93
lines changed

3 files changed

+23
-93
lines changed

articles/key-vault/general/vs-key-vault-add-connected-service.md

Lines changed: 23 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -7,121 +7,51 @@ manager: jillfra
77
ms.service: key-vault
88
ms.custom: "vs-azure, devx-track-csharp"
99
ms.topic: how-to
10-
ms.date: 08/07/2019
10+
ms.date: 11/14/2022
1111
ms.author: ghogen
1212
---
1313
# Add Key Vault to your web application by using Visual Studio Connected Services
1414

1515
In this tutorial, you will learn how to easily add everything you need to start using Azure Key Vault to manage your secrets for web projects in Visual Studio, whether you are using ASP.NET Core or any type of ASP.NET project. By using the Connected Services feature in Visual Studio, you can have Visual Studio automatically add all the NuGet packages and configuration settings you need to connect to Key Vault in Azure.
1616

17-
For details on the changes that Connected Services makes in your project to enable Key Vault, see [Key Vault Connected Service - What happened to my ASP.NET 4.7.1 project](#how-your-aspnet-framework-project-is-modified) or [Key Vault Connected Service - What happened to my ASP.NET Core project](#how-your-aspnet-core-project-is-modified).
17+
For details on the changes that Connected Services makes in your project to enable Key Vault, see [Key Vault Connected Service - What happened to my ASP.NET project](#how-your-aspnet-framework-project-is-modified) or [Key Vault Connected Service - What happened to my ASP.NET Core project](#how-your-aspnet-core-project-is-modified).
1818

1919
## Prerequisites
2020

2121
- **An Azure subscription**. If you don't have a subscription, sign up for a [free account](https://azure.microsoft.com/pricing/free-trial/).
2222
- **Visual Studio 2019 version 16.3** or later [Download it now](https://aka.ms/vsdownload?utm_source=mscom&utm_campaign=msdocs).
2323

24-
2524
## Add Key Vault support to your project
2625

27-
Before you begin, make sure that you're signed into Visual Studio. Sign in with the same account that you use for your Azure subscription. Then open an ASP.NET 4.7.1 or later, or ASP.NET Core 2.0 web project, and do the follow steps:
26+
Before you begin, make sure that you're signed into Visual Studio. Sign in with the same account that you use for your Azure subscription. Then open an ASP.NET 4.7.1 or later, or ASP.NET Core web project, and do the following steps. The steps shown are for Visual Studio 2022 version 17.4. The flow might be slightly different for other versions of Visual Studio.
2827

29-
1. In **Solution Explorer**, right-click the project that you want to add the Key Vault support to, and choose **Add** > **Connected Service** > **Add**.
28+
1. In **Solution Explorer**, right-click the project that you want to add the Key Vault support to, and choose **Add** > **Connected Service**. Under **Service Dependencies**, choose the **+** icon.
3029
The Connected Service page appears with services you can add to your project.
3130
1. In the menu of available services, choose **Azure Key Vault** and click **Next**.
3231

3332
![Choose "Azure Key Vault"](../media/vs-key-vault-add-connected-service/key-vault-connected-service.png)
3433

35-
1. Select the subscription you want to use, and then choose a existing Key Vault and click **Finish**.
36-
37-
![Select your subscription](../media/vs-key-vault-add-connected-service/key-vault-connected-service-select-vault.png)
38-
39-
Now, connection to Key Vault is established and you can access your secrets in code. The next steps are different depending on whether you are using ASP.NET 4.7.1 or ASP.NET Core.
40-
41-
## Access your secrets in code (ASP.NET Core)
42-
43-
1. Open one of the page files, such as *Index.cshtml.cs* and write the following code:
44-
1. Include a reference to `Microsoft.Extensions.Configuration` by this using directive:
45-
46-
```csharp
47-
using Microsoft.Extensions.Configuration;
48-
```
49-
50-
1. Add the configuration variable.
51-
52-
```csharp
53-
private static IConfiguration _configuration;
54-
```
55-
56-
1. Add this constructor or replace the existing constructor with this:
57-
58-
```csharp
59-
public IndexModel(IConfiguration configuration)
60-
{
61-
_configuration = configuration;
62-
}
63-
```
64-
65-
1. Update the `OnGet` method. Update the placeholder value shown here with the secret name you created in the above commands.
66-
67-
```csharp
68-
public void OnGet()
69-
{
70-
ViewData["Message"] = "My key val = " + _configuration["<YourSecretNameStoredInKeyVault>"];
71-
}
72-
```
73-
74-
1. To confirm the value at runtime, add code to display `ViewData["Message"]` to the *.cshtml* file to display the secret in a message.
75-
76-
```cshtml
77-
<p>@ViewData["Message"]</p>
78-
```
79-
80-
You can run the app locally to verify that the secret is obtained successfully from the Key Vault.
81-
82-
## Access your secrets (ASP.NET)
83-
You can set up the configuration so that the web.config file has a dummy value in the `appSettings` element that is replaced by the true value at runtime. You can then access this via the `ConfigurationManager.AppSettings` data structure.
84-
85-
1. In Solution Explorer, right-click on your project, and select Manage NuGet Packages. In the Browse tab, locate and install [Microsoft.Configuration.ConfigurationBuilders.Azure](https://www.nuget.org/packages/Microsoft.Configuration.ConfigurationBuilders.Azure/)
86-
87-
1. Open your web.config file, and write the following code:
88-
1. Add `configSections` and `configBuilders`:
89-
```xml
90-
<configSections>
91-
<section
92-
name="configBuilders"
93-
type="System.Configuration.ConfigurationBuildersSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
94-
restartOnExternalChanges="false"
95-
requirePermission="false" />
96-
</configSections>
97-
<configBuilders>
98-
<builders>
99-
<add
100-
name="AzureKeyVault"
101-
vaultName="vaultname"
102-
type="Microsoft.Configuration.ConfigurationBuilders.AzureKeyVaultConfigBuilder, Microsoft.Configuration.ConfigurationBuilders.Azure, Version=1.0.0.0, Culture=neutral"
103-
vaultUri="https://vaultname.vault.azure.net" />
104-
</builders>
105-
</configBuilders>
106-
```
107-
1. Find the appSettings tag, add an attribute `configBuilders="AzureKeyVault"`, and add a line:
108-
```xml
109-
<add key="<secretNameInYourKeyVault>" value="dummy"/>
110-
```
111-
112-
1. Edit the `About` method in *HomeController.cs*, to display the value for confirmation.
113-
114-
```csharp
115-
public ActionResult About()
116-
{
117-
ViewBag.Message = "Key vault value = " + ConfigurationManager.AppSettings["<secretNameInYourKeyVault>"];
118-
}
119-
```
120-
1. Run the app locally under the debugger, switch to the **About** tab, and verify that the value from the Key Vault is displayed.
34+
1. Select the subscription you want to use, and then if you already have a Key Vault you want to use, select it and click **Next**.
35+
36+
![Screenshot Select your subscription](../media/vs-key-vault-add-connected-service/key-vault-connected-service-select-vault.png)
37+
38+
1. If you don't have an existing Key Vault, click on **Create new Key Vault**. You'll be asked to provide the resource group, location, and SKU.
39+
40+
![Screenshot of "Create Azure Key Vault" screen](../media/vs-key-vault-add-connected-service/create-new-key-vault.png)
41+
42+
1. In the **Configure Key Vault** screen, you can change the name of the environment variable.
43+
44+
![Screenshot of Connect to Azure Key Vault screen.](../media/vs-key-vault-add-connected-service/connect-to-azure-key-vault.png)
45+
46+
1. Click **Next** to review a summary of the changes and then **Finish**.
47+
48+
Now, connection to Key Vault is established and you can access your secrets in code. If you just created a new key vault, test it by creating a secret that you can reference in code. You can create a secret by using the [Azure portal](../secrets/quick-create-portal.md), [PowerShell](../secrets/quick-create-powershell.md), or the [Azure CLI](../secrets/quick-create-cli.md).
49+
50+
See code examples of working with secrets at [Azure Key Vault Secrets client library for .NET - Code examples](../secrets/quick-create-net.md?tabs=azure-cli#code-examples).
12151

12252
## Troubleshooting
12353

124-
If your Key Vault is running on an different Microsoft account than the one you're logged in to Visual Studio (for example, the Key Vault is running on your work account, but Visual Studio is using your private account) you get an error in your Program.cs file, that Visual Studio can't get access to the Key Vault. To fix this issue:
54+
If your Key Vault is running on a different Microsoft account than the one you're logged in to Visual Studio (for example, the Key Vault is running on your work account, but Visual Studio is using your private account) you get an error in your Program.cs file, that Visual Studio can't get access to the Key Vault. To fix this issue:
12555

12656
1. Go to the [Azure portal](https://portal.azure.com) and open your Key Vault.
12757

@@ -200,4 +130,4 @@ Affects the project file .NET references and `packages.config` (NuGet references
200130

201131
If you followed this tutorial, your Key Vault permissions are set up to run with your own Azure subscription, but that might not be desirable for a production scenario. You can create a managed identity to manage Key Vault access for your app. See [How to Authenticate to Key Vault](./authentication.md) and [Assign a Key Vault access policy](./assign-access-policy-portal.md).
202132

203-
Learn more about Key Vault development by reading the [Key Vault Developer's Guide](developers-guide.md).
133+
Learn more about Key Vault development by reading the [Key Vault Developer's Guide](developers-guide.md).
34.1 KB
Loading
50.3 KB
Loading

0 commit comments

Comments
 (0)