You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: learn-pr/azure/manage-secrets-with-azure-key-vault/includes/5-access-secrets-from-web-app.md
+24-40Lines changed: 24 additions & 40 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -83,58 +83,42 @@ To demonstrate good use of Key Vault, modify your app to load secrets from the v
83
83
1. Open `Program.cs`, delete the contents, and replace them with the following code.
84
84
85
85
```csharp
86
-
using System;
87
86
using Azure.Identity;
88
-
using Microsoft.AspNetCore;
89
-
using Microsoft.AspNetCore.Hosting;
90
-
using Microsoft.Extensions.Configuration;
91
-
using Microsoft.Extensions.Hosting;
92
87
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);
101
89
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();
127
111
```
128
112
129
113
> [!IMPORTANT]
130
114
> 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).
131
115
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.
133
117
134
118
1. For the controller, create a new file in the `Controllers` folder called `SecretTestController.cs`, and paste in the following code.
135
119
136
120
> [!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.
0 commit comments