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
This tutorial illustrates how to use a [managed identity](../../active-directory/managed-identities-azure-resources/overview.md) to authenticate an Azure Web App with an Azure Key Vault. You can use a managed identity to authenticate to any service that supports Azure AD authentication, including Key Vault, without any credentials in your code.
29
21
30
-
This quickstart shows how to create a [.NET Core](https://docs.microsoft.com/aspnet/core/) webapp, and connect to an Azure Key Vault through the use of a Managed Identity. You will use the [Azure CLI](https://docs.microsoft.com/cli/azure/get-started-with-azure-cli) to create the app and Git to deploy the .NET Core code to the app.
22
+
> [!NOTE]
23
+
>
24
+
> This tutorial uses the [Azure Key Vault v4 client library for .NET](/dotnet/api/overview/azure/key-vault?view=azure-dotnet) and the [Azure CLI](/cli/azure/get-started-with-azure-cli). However, the same basic principles apply when using the development language of your choice and/or Azure PowerShell.
31
25
32
-
You can follow the steps in this article using a Mac, Windows, or Linux machine.
@@ -47,32 +40,34 @@ This quickstart assumes you are running `dotnet`, [Azure CLI](/cli/azure/install
47
40
48
41
A resource group is a logical container into which Azure resources are deployed and managed.
49
42
50
-
Your first step is to create a resource group to house both your key vault and your webapp. You can do so with the [az group create](/cli/azure/group?view=azure-cli-latest#az-group-create) command:
43
+
Your first step is to create a resource group to house both your key vault and your web app. You can do so with the [az group create](/cli/azure/group?view=azure-cli-latest#az-group-create) command:
51
44
52
45
```azurecli
53
46
az group create --name "myResourceGroup" -l "EastUS"
54
47
```
55
48
56
49
## Set up your key vault
57
50
58
-
You will now create a key vault and place a secret in it, for use later in this quickstart.
51
+
You will now create a key vault and place a secret in it, for use later in this tutorial.
59
52
60
53
To create a key vault, use the [az keyvault create](/cli/azure/keyvault?view=azure-cli-latest#az-keyvault-create) command:
61
54
62
55
> [!Important]
63
56
> Each key vault must have a unique name. Replace <your-unique-keyvault-name> with the name of your key vault in the following examples.
64
57
65
58
```azurecli
66
-
az keyvault create --name <your-unique-keyvault-name> -g "myResourceGroup"
59
+
az keyvault create --name "<your-unique-keyvault-name>" -g "myResourceGroup"
67
60
```
68
61
62
+
Make a note of the returned `vaultUri`, which will be in the format"https://<your-unique-keyvault-name>.vault.azure.net/". It will be used in the [Update the code](#update-the-code) step.
63
+
69
64
You can now place a secret in your key vault with the [az keyvault secret set](/cli/azure/keyvault/secret?view=azure-cli-latest#az-keyvault-secret-set) command. Set the name of your secret to MySecret and the value to "Success!".
70
65
71
66
```azurecli
72
-
az keyvault secret set --vault-name <your-unique-keyvault-name> -name "MySecret" -value "Success!"
67
+
az keyvault secret set --vault-name "<your-unique-keyvault-name>" --name "MySecret" --value "Success!"
73
68
```
74
69
75
-
## Create a .NET webapp
70
+
## Create a .NET web app
76
71
77
72
### Create a local app
78
73
@@ -89,8 +84,6 @@ Now create a new .NET Core app with the [dotnet new web](/dotnet/core/tools/dotn
89
84
dotnet new web
90
85
```
91
86
92
-
### Run the local app
93
-
94
87
Run the application locally so that you see how it should look when you deploy it to Azure.
95
88
96
89
```bash
@@ -99,11 +92,11 @@ dotnet run
99
92
100
93
Open a web browser, and navigate to the app at `http://localhost:5000`.
101
94
102
-
You see the **Hello World** message from the sample app displayed in the page.
95
+
You will see the **Hello World** message from the sample app displayed in the page.
103
96
104
-

97
+
### Initialize the git repository
105
98
106
-
In your terminal window, press **Ctrl+C** to exit the web server. Initialize a Git repository for the .NET Core project.
99
+
In your terminal window, press **Ctrl+C** to exit the web server. Initialize a git repository for the .NET Core project.
FTP and local Git can deploy to an Azure webapp by using a *deployment user*. Once you configure your deployment user, you can use it for all your Azure deployments. Your account-level deployment username and password are different from your Azure subscription credentials.
109
+
FTP and local Git can deploy to an Azure web app by using a *deployment user*. Once you configure your deployment user, you can use it for all your Azure deployments. Your account-level deployment username and password are different from your Azure subscription credentials.
117
110
118
-
To configure the deployment user, run the [az webapp deployment user set](/cli/azure/webapp/deployment/user?view=azure-cli-latest#az-webapp-deployment-user-set) command in Azure Cloud Shell. Replace \<username> and \<password> with a deployment user username and password.
111
+
To configure the deployment user, run the [az webapp deployment user set](/cli/azure/webapp/deployment/user?view=azure-cli-latest#az-webapp-deployment-user-set) command. Choose a username and password that adheres to these guidelines:
119
112
120
113
- The username must be unique within Azure, and for local Git pushes, must not contain the ‘@’ symbol.
121
114
- The password must be at least eight characters long, with two of the following three elements: letters, numbers, and symbols.
122
115
123
116
```azurecli-interactive
124
-
az webapp deployment user set --user-name <username> --password <password>
117
+
az webapp deployment user set --user-name "<username>" --password "<password>"
125
118
```
126
119
127
120
The JSON output shows the password as `null`. If you get a `'Conflict'. Details: 409` error, change the username. If you get a `'Bad Request'. Details: 400` error, use a stronger password.
128
121
129
-
Record your username and password to use to deploy your webapps.
122
+
Record your username and password to use to deploy your web apps.
130
123
131
124
### Create an app service plan
132
125
133
-
Create an App Service plan with the Azure CLI [az appservice plan create](/cli/azure/appservice/plan?view=azure-cli-latest) command.
134
-
135
-
The following example creates an App Service plan named `myAppServicePlan` in the **Free** pricing tier:
126
+
Create an App Service plan with the Azure CLI [az appservice plan create](/cli/azure/appservice/plan?view=azure-cli-latest) command. This following example creates an App Service plan named `myAppServicePlan` in the **Free** pricing tier:
136
127
137
128
```azurecli-interactive
138
129
az appservice plan create --name myAppServicePlan --resource-group myResourceGroup --sku FREE
@@ -159,15 +150,19 @@ When the App Service plan has been created, the Azure CLI shows information simi
159
150
</pre>
160
151
161
152
162
-
### Create a remote webapp
153
+
### Create a remote web app
154
+
155
+
Create an [Azure web app](../../app-service/containers/app-service-linux-intro.md) in the `myAppServicePlan` App Service plan.
156
+
157
+
> [!Important]
158
+
> Similar to Key Vault, an Azure Web App must have a unique name. Replace <your-webapp-name> with the name of your web app the following examples.
163
159
164
-
Create a [webapp](../../app-service/containers/app-service-linux-intro.md) in the `myAppServicePlan` App Service plan.
165
160
166
161
```azurecli-interactive
167
-
az webapp create --resource-group myResourceGroup --plan myAppServicePlan --name <your-webapp-name> --deployment-local-git
162
+
az webapp create --resource-group "myResourceGroup" --plan "myAppServicePlan" --name "<your-webapp-name>" --deployment-local-git
168
163
```
169
164
170
-
When the webapp has been created, the Azure CLI shows output similar to the following example:
165
+
When the web app has been created, the Azure CLI shows output similar to the following example:
171
166
172
167
<pre>
173
168
Local git is configured with url of 'https://<username>@<your-webapp-name>.scm.azurewebsites.net/<ayour-webapp-name>.git'
@@ -186,28 +181,26 @@ Local git is configured with url of 'https://<username>@<your-webapp-na
186
181
}
187
182
</pre>
188
183
189
-
> [!NOTE]
190
-
> The URL of the Git remote is shown in the `deploymentLocalGitUrl` property, with the format `https://<username>@<your-webapp-name>.scm.azurewebsites.net/<your-webapp-name>.git`. Save this URL as you need it later.
191
-
>
184
+
185
+
The URL of the Git remote is shown in the `deploymentLocalGitUrl` property, with the format `https://<username>@<your-webapp-name>.scm.azurewebsites.net/<your-webapp-name>.git`. Save this URL, as you need it later.
192
186
193
187
Browse to your newly created app. Replace _<your-webapp-name>_ with your app name.
You will see the default webpage for a newly created Azure Web App.
202
194
195
+
### Deploy your local app
203
196
204
-
Back in the local terminal window, add an Azure remote to your local Git repository. Replace *\<deploymentLocalGitUrl-from-create-step>* with the URL of the Git remote that you saved from [Create a remote webapp](#create-a-remote-webapp).
197
+
Back in the local terminal window, add an Azure remote to your local Git repository. Replace *\<deploymentLocalGitUrl-from-create-step>* with the URL of the Git remote that you saved from [Create a remote web app](#create-a-remote-web-app).
Push to the Azure remote to deploy your app with the following command. When Git Credential Manager prompts you for credentials, make sure you enter the credentials you created in [Configure a deployment user](/azure/app-service/containers/tutorial-python-postgresql-app#configure-a-deployment-user), not the credentials you use to sign in to the Azure portal.
203
+
Push to the Azure remote to deploy your app with the following command. When Git Credential Manager prompts you for credentials, use the credentials you created in [Configure a deployment user](#configure-a-deployment-user) step.
211
204
212
205
```bash
213
206
git push azure master
@@ -241,17 +234,13 @@ To https://<your-webapp-name>.scm.azurewebsites.net:443/<your-webapp-na
241
234
d87e6ca..d6b5447 master -> master
242
235
</pre>
243
236
244
-
### Browse to the app
245
-
246
-
Browse to the deployed application using your web browser.
237
+
Browse to (or refresh) the deployed application using your web browser.
247
238
248
239
```bash
249
240
http://<your-webapp-name>.azurewebsites.net
250
241
```
251
242
252
-
The .NET Core sample code is running in App Service on Linux with a built-in image.
253
-
254
-

243
+
You will see the "Hello World!" message you previously saw when visiting `http://localhost:5000`.
255
244
256
245
## Create and assign a managed identity
257
246
@@ -260,7 +249,7 @@ Azure Key Vault provides a way to securely store credentials and other secrets,
260
249
In the Azure CLI, to create the identity for this application, run the [az webapp-identity assign](/cli/azure/webapp/identity?view=azure-cli-latest#az-webapp-identity-assign) command:
261
250
262
251
```azurecli
263
-
az webapp identity assign --name "<YourAppName>" --resource-group "<YourResourceGroupName>"
252
+
az webapp identity assign --name "<your-webapp-name>" --resource-group "myResourceGroup"
264
253
```
265
254
266
255
The operation will return this JSON snippet:
@@ -273,10 +262,10 @@ The operation will return this JSON snippet:
273
262
}
274
263
```
275
264
276
-
To give your webapp permission to do **get** and **list** operations on your key vault, pass the principalID to the Azure CLI [az keyvault set-policy](/cli/azure/keyvault?view=azure-cli-latest#az-keyvault-set-policy) command:
265
+
To give your web app permission to do **get** and **list** operations on your key vault, pass the principalID to the Azure CLI [az keyvault set-policy](/cli/azure/keyvault?view=azure-cli-latest#az-keyvault-set-policy) command:
277
266
278
267
```azurecli
279
-
az keyvault set-policy --name "<your-unique-keyvault-name>" --object-id <principalId> --secret-permissions get list
268
+
az keyvault set-policy --name "<your-unique-keyvault-name>" --object-id "<principalId>" --secret-permissions get list
Find and open the Startup.cs file in your akvwebapp project. Add these three lines before the `app.UseEndpoints` call, updating the URI to reflect your unique webapp name:
285
+
Find and open the Startup.cs file in your akvwebapp project.
286
+
287
+
Add these two lines to the header:
288
+
289
+
```cpp
290
+
using Azure.Identity;
291
+
using Azure.Security.KeyVault.Secrets;
292
+
```
293
+
294
+
Add these three lines before the `app.UseEndpoints` call, updating the URI to reflect the `vaultUri` of your key vault.
297
295
298
296
```cpp
299
-
var client = new SecretClient(new Uri("https://<your-webapp-name>.vault.azure.net/"), new DefaultAzureCredential());
297
+
var client = new SecretClient(new Uri("https://<your-unique-key-vault-name>.vault.azure.net/"), new DefaultAzureCredential());
Copy file name to clipboardExpand all lines: articles/key-vault/secrets/quick-create-net.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -27,7 +27,7 @@ Azure Key Vault helps safeguard cryptographic keys and secrets used by cloud app
27
27
## Prerequisites
28
28
29
29
* An Azure subscription - [create one for free](https://azure.microsoft.com/free/?WT.mc_id=A261C142F).
30
-
* The [.NET Core 2.1 SDK or later](https://dotnet.microsoft.com/download/dotnet-core/2.1).
30
+
* The [.NET Core 3.1 SDK or later](https://dotnet.microsoft.com/download/dotnet-core/2.1).
31
31
*[Azure CLI](/cli/azure/install-azure-cli?view=azure-cli-latest) or [Azure PowerShell](/powershell/azure/overview)
32
32
33
33
This quickstart assumes you are running `dotnet`, [Azure CLI](/cli/azure/install-azure-cli?view=azure-cli-latest), and Windows commands in a Windows terminal (such as [PowerShell Core](/powershell/scripting/install/installing-powershell-core-on-windows?view=powershell-6), [Windows PowerShell](/powershell/scripting/install/installing-windows-powershell?view=powershell-6), or the [Azure Cloud Shell](https://shell.azure.com/)).
0 commit comments