This article provides additional guidance after you’ve added Key Vault to an ASP.NET Core project through the Add Connected Services command in Visual Studio. If you’ve not already added the service to your project, you can do so at any time by following the instructions in Add Key Vault to your web application by using Visual Studio Connected Services.
See What happened to my ASP.NET Core project? for the changes made to your project when adding the connected service.
After you connect
Add a secret in your Key Vault in Azure. To get to the right place in the portal, click on the link for Manage secrets stored in this Key Vault. If you closed the page or the project, you can navigate to it in the Azure portal by choosing All Services, under Security, choose Key Vault, then choose the Key Vault you just created.
In the Key Vault section for the key vault you created, choose Secrets, then Generate/Import.
Enter a secret, such as “MySecret”, and give it any string value as a test, then choose the Create button.
(optional) Enter another secret, but this time put it into a category by naming it Secrets–MySecret. This syntax specifies a category Secrets that contains a secret MySecret.
In your Visual Studio project, you can now reference these secrets by using the following expressions in code:
config["MySecret"] // Access a secret without a section config["Secrets:MySecret"] // Access a secret in a section config.GetSection("Secrets")["MySecret"] // Get the configuration section and access a secret in it.
Congratulations, you have now enabled your web app to use Key Vault to access securely stored secrets.
Clean up resources
When no longer needed, delete the resource group. This deletes the Key Vault and related resources. To delete the resource group through the portal:
- Enter the name of your resource group in the Search box at the top of the portal. When you see the resource group used in this QuickStart in the search results, select it.
- Select Delete resource group.
- In the TYPE THE RESOURCE GROUP NAME: box type in the name of the resource group and select Delete.
Learn more about developing with Key Vault in the Key Vault Developer’s Guide