Skip to content

Commit 2b686b2

Browse files
authored
Merge pull request #53630 from msmbaldwin/fix-keyvault-exercise-net6
Fix Key Vault exercise for .NET 6+ minimal hosting
2 parents 991df8c + 6ff4c19 commit 2b686b2

2 files changed

Lines changed: 44 additions & 60 deletions

File tree

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
### YamlMime:ModuleUnit
2-
uid: learn.manage-secrets-with-azure-key-vault.5-access-secrets-from-web-app
3-
title: Exercise - Access secrets stored in Azure Key Vault
4-
metadata:
5-
title: Exercise - Access secrets stored in Azure Key Vault
6-
description: Exercise - Access secrets stored in Azure Key Vault
7-
ms.date: 12/12/2024
8-
author: msmbaldwin
9-
ms.author: mbaldwin
10-
ms.topic: unit
11-
ms.custom:
12-
- devx-track-extended-java
13-
- devx-track-js
14-
zone_pivot_groups: dev-lang-csharp-javascript
15-
durationInMinutes: 13
16-
#interactive: bash
17-
#azureSandbox: true
18-
content: |
19-
[!include[](includes/5-access-secrets-from-web-app.md)]
20-
1+
### YamlMime:ModuleUnit
2+
uid: learn.manage-secrets-with-azure-key-vault.5-access-secrets-from-web-app
3+
title: Exercise - Access secrets stored in Azure Key Vault
4+
metadata:
5+
title: Exercise - Access secrets stored in Azure Key Vault
6+
description: Exercise - Access secrets stored in Azure Key Vault
7+
ms.date: 02/25/2026
8+
author: msmbaldwin
9+
ms.author: mbaldwin
10+
ms.topic: unit
11+
ms.custom:
12+
- devx-track-extended-java
13+
- devx-track-js
14+
zone_pivot_groups: dev-lang-csharp-javascript
15+
durationInMinutes: 13
16+
#interactive: bash
17+
#azureSandbox: true
18+
content: |
19+
[!include[](includes/5-access-secrets-from-web-app.md)]
20+

learn-pr/azure/manage-secrets-with-azure-key-vault/includes/5-access-secrets-from-web-app.md

Lines changed: 24 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -83,58 +83,42 @@ To demonstrate good use of Key Vault, modify your app to load secrets from the v
8383
1. Open `Program.cs`, delete the contents, and replace them with the following code.
8484

8585
```csharp
86-
using System;
8786
using Azure.Identity;
88-
using Microsoft.AspNetCore;
89-
using Microsoft.AspNetCore.Hosting;
90-
using Microsoft.Extensions.Configuration;
91-
using Microsoft.Extensions.Hosting;
9287

93-
namespace KeyVaultDemoApp
94-
{
95-
public class Program
96-
{
97-
public static void Main(string[] args)
98-
{
99-
CreateHostBuilder(args).Build().Run();
100-
}
88+
var builder = WebApplication.CreateBuilder(args);
10189

102-
public static IHostBuilder CreateHostBuilder(string[] args) =>
103-
Host.CreateDefaultBuilder(args)
104-
.ConfigureWebHostDefaults(webBuilder =>
105-
{
106-
webBuilder.UseStartup<Startup>();
107-
})
108-
.ConfigureAppConfiguration((context, config) =>
109-
{
110-
// Build the current set of configuration to load values from
111-
// JSON files and environment variables, including VaultName.
112-
var builtConfig = config.Build();
113-
114-
// Use VaultName from the configuration to create the full vault URI.
115-
var vaultName = builtConfig["VaultName"];
116-
Uri vaultUri = new Uri($"https://{vaultName}.vault.azure.net/");
117-
118-
// Load all secrets from the vault into configuration. This will automatically
119-
// authenticate to the vault using a managed identity. If a managed identity
120-
// is not available, it will check if Visual Studio and/or the Azure CLI are
121-
// installed locally and see if they are configured with credentials that can
122-
// access the vault.
123-
config.AddAzureKeyVault(vaultUri, new DefaultAzureCredential());
124-
});
125-
}
126-
}
90+
// Get the vault name from configuration
91+
var vaultName = builder.Configuration["VaultName"];
92+
var vaultUri = new Uri($"https://{vaultName}.vault.azure.net/");
93+
94+
// Load all secrets from the vault into configuration. This will automatically
95+
// authenticate to the vault using a managed identity. If a managed identity
96+
// is not available, it will check if Visual Studio and/or the Azure CLI are
97+
// installed locally and see if they are configured with credentials that can
98+
// access the vault.
99+
builder.Configuration.AddAzureKeyVault(vaultUri, new DefaultAzureCredential());
100+
101+
// Add services to the container
102+
builder.Services.AddControllers();
103+
104+
var app = builder.Build();
105+
106+
app.UseHttpsRedirection();
107+
app.UseAuthorization();
108+
app.MapControllers();
109+
110+
app.Run();
127111
```
128112

129113
> [!IMPORTANT]
130114
> Make sure to save files when you're done editing them. You can save files either through the "..." menu, or the accelerator key (**Ctrl**+**S** on Windows and Linux, **Cmd**+**S** on macOS).
131115

132-
The only change from the starter code is the addition of `ConfigureAppConfiguration`. This element is where we load the vault name from configuration and call `AddAzureKeyVault` with it.
116+
This code uses the .NET minimal hosting model. It loads the vault name from configuration and calls `AddAzureKeyVault` to load all secrets from the vault at startup.
133117

134118
1. For the controller, create a new file in the `Controllers` folder called `SecretTestController.cs`, and paste in the following code.
135119

136120
> [!TIP]
137-
> To create a new file, use the `touch` command in Cloud Shell. In this case, run the `touch Controllers/SecretTestController.cs` command. To find it in the top right corner of the *Files* pane of the editor, select the **Refresh** icon.
121+
> To create a new file, use the `mkdir` and `touch` commands in Cloud Shell. In this case, run `mkdir Controllers && touch Controllers/SecretTestController.cs`. To find it in the top right corner of the *Files* pane of the editor, select the **Refresh** icon.
138122

139123
```csharp
140124
using System;

0 commit comments

Comments
 (0)