Skip to content

Commit 33f5f10

Browse files
committed
Updated
1 parent ce05fc0 commit 33f5f10

File tree

2 files changed

+66
-67
lines changed

2 files changed

+66
-67
lines changed

articles/key-vault/general/tutorial-net-create-vault-azure-web-app.md

Lines changed: 65 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,21 @@ manager: rajvijan
88
ms.service: key-vault
99
ms.subservice: general
1010
ms.topic: tutorial
11-
ms.date: 12/21/2018
11+
ms.date: 05/06/2020
1212
ms.author: mbaldwin
13-
ms.custom: mvc
14-
#Customer intent: As a developer I want to use Azure Key Vault to store secrets for my app, so that they are kept secure.
15-
---
16-
# Tutorial: Use Azure Key Vault with an Azure webapp in .NET
1713

18-
Get started with the Azure Key Vault client library for .NET. Follow the steps below to install the package and try out example code for basic tasks.
14+
#Customer intent: As a developer I want to use Azure Key Vault to store secrets for my app, so that they are kept secure.
1915

20-
Azure Key Vault helps safeguard cryptographic keys and secrets used by cloud applications and services. Use the Key Vault client library for .NET to:
16+
---
2117

22-
- Increase security and control over keys and passwords.
23-
- Create and import encryption keys in minutes.
24-
- Reduce latency with cloud scale and global redundancy.
25-
- Simplify and automate tasks for TLS/SSL certificates.
26-
- Use FIPS 140-2 Level 2 validated HSMs.
18+
# Tutorial: Use a managed identity to connect a key vault to an Azure Web App in .NET
2719

28-
[API reference documentation](/dotnet/api/overview/azure/key-vault?view=azure-dotnet) | [Library source code](https://github.com/Azure/azure-sdk-for-net/tree/master/sdk/keyvault) | [Package (NuGet)](https://www.nuget.org/packages/Microsoft.Azure.KeyVault/)
20+
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.
2921

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.
3125
32-
You can follow the steps in this article using a Mac, Windows, or Linux machine.
3326

3427
[!INCLUDE [quickstarts-free-trial-note](../../../includes/quickstarts-free-trial-note.md)]
3528

@@ -47,32 +40,34 @@ This quickstart assumes you are running `dotnet`, [Azure CLI](/cli/azure/install
4740

4841
A resource group is a logical container into which Azure resources are deployed and managed.
4942

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:
5144

5245
```azurecli
5346
az group create --name "myResourceGroup" -l "EastUS"
5447
```
5548

5649
## Set up your key vault
5750

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.
5952

6053
To create a key vault, use the [az keyvault create](/cli/azure/keyvault?view=azure-cli-latest#az-keyvault-create) command:
6154

6255
> [!Important]
6356
> Each key vault must have a unique name. Replace <your-unique-keyvault-name> with the name of your key vault in the following examples.
6457
6558
```azurecli
66-
az keyvault create --name <your-unique-keyvault-name> -g "myResourceGroup"
59+
az keyvault create --name "<your-unique-keyvault-name>" -g "myResourceGroup"
6760
```
6861

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+
6964
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!".
7065

7166
```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!"
7368
```
7469

75-
## Create a .NET webapp
70+
## Create a .NET web app
7671

7772
### Create a local app
7873

@@ -89,8 +84,6 @@ Now create a new .NET Core app with the [dotnet new web](/dotnet/core/tools/dotn
8984
dotnet new web
9085
```
9186

92-
### Run the local app
93-
9487
Run the application locally so that you see how it should look when you deploy it to Azure.
9588

9689
```bash
@@ -99,11 +92,11 @@ dotnet run
9992

10093
Open a web browser, and navigate to the app at `http://localhost:5000`.
10194

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.
10396

104-
![Test with browser](media/quickstart-dotnetcore/dotnet-browse-local.png)
97+
### Initialize the git repository
10598

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.
107100

108101
```bash
109102
git init
@@ -113,26 +106,24 @@ git commit -m "first commit"
113106

114107
### Configure a deployment user
115108

116-
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.
117110

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:
119112

120113
- The username must be unique within Azure, and for local Git pushes, must not contain the ‘@’ symbol.
121114
- The password must be at least eight characters long, with two of the following three elements: letters, numbers, and symbols.
122115

123116
```azurecli-interactive
124-
az webapp deployment user set --user-name <username> --password <password>
117+
az webapp deployment user set --user-name "<username>" --password "<password>"
125118
```
126119

127120
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.
128121

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.
130123

131124
### Create an app service plan
132125

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:
136127

137128
```azurecli-interactive
138129
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
159150
</pre>
160151

161152

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.
163159
164-
Create a [webapp](../../app-service/containers/app-service-linux-intro.md) in the `myAppServicePlan` App Service plan.
165160

166161
```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
168163
```
169164

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:
171166

172167
<pre>
173168
Local git is configured with url of 'https://&lt;username&gt;@&lt;your-webapp-name&gt;.scm.azurewebsites.net/&lt;ayour-webapp-name&gt;.git'
@@ -186,28 +181,26 @@ Local git is configured with url of 'https://&lt;username&gt;@&lt;your-webapp-na
186181
}
187182
</pre>
188183

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.
192186

193187
Browse to your newly created app. Replace _&lt;your-webapp-name>_ with your app name.
194188

195189
```bash
196190
https://<your-webapp-name>.azurewebsites.net
197191
```
198192

199-
Here is what your new app should look like:
200-
201-
![Empty app page](media/quickstart-dotnetcore/dotnet-browse-created.png)
193+
You will see the default webpage for a newly created Azure Web App.
202194

195+
### Deploy your local app
203196

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).
205198

206199
```bash
207200
git remote add azure <deploymentLocalGitUrl-from-create-step>
208201
```
209202

210-
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.
211204

212205
```bash
213206
git push azure master
@@ -241,17 +234,13 @@ To https://&lt;your-webapp-name&gt;.scm.azurewebsites.net:443/&lt;your-webapp-na
241234
d87e6ca..d6b5447 master -> master
242235
</pre>
243236

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.
247238

248239
```bash
249240
http://<your-webapp-name>.azurewebsites.net
250241
```
251242

252-
The .NET Core sample code is running in App Service on Linux with a built-in image.
253-
254-
![Sample app running in Azure](media/quickstart-dotnetcore/dotnet-browse-azure.png)
243+
You will see the "Hello World!" message you previously saw when visiting `http://localhost:5000`.
255244

256245
## Create and assign a managed identity
257246

@@ -260,7 +249,7 @@ Azure Key Vault provides a way to securely store credentials and other secrets,
260249
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:
261250

262251
```azurecli
263-
az webapp identity assign --name "<YourAppName>" --resource-group "<YourResourceGroupName>"
252+
az webapp identity assign --name "<your-webapp-name>" --resource-group "myResourceGroup"
264253
```
265254

266255
The operation will return this JSON snippet:
@@ -273,10 +262,10 @@ The operation will return this JSON snippet:
273262
}
274263
```
275264

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:
277266

278267
```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
280269
```
281270

282271

@@ -293,38 +282,48 @@ dotnet add package Azure.Security.KeyVault.Secrets
293282

294283
### Update the code
295284

296-
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.
297295

298296
```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());
298+
300299
KeyVaultSecret secret = client.GetSecret("mySecret");
300+
301301
string secretValue = secret.Value;
302302
```
303303

304+
Update the line `await context.Response.WriteAsync("Hello World!");` to read:
305+
306+
```cpp
307+
await context.Response.WriteAsync(secretValue);
308+
```
309+
304310
Be certain to save your changes before proceeding to the next step.
305311

306-
### Redeploy your webapp
312+
### Redeploy your web app
307313

308314
Having updated your code, you can redeploy it to Azure with the following git commands:
309315

310316
```bash
311317
git add .
312-
git commit -m "Updated webapp to access my key vault"
318+
git commit -m "Updated web app to access my key vault"
313319
git push azure master
314320
```
315321

316-
## Visit your completed webapp
322+
## Visit your completed web app
317323

318324
```bash
319325
http://<your-webapp-name>.azurewebsites.net
320326
```
321327

322-
Where before you saw "Hello world!", you shoukd now see the value of your secret displayed:
323-
324-
325-
326-
327-
328-
329-
328+
Where before you saw "Hello world!", you shoukd now see the value of your secret displayed: Success!
330329

articles/key-vault/secrets/quick-create-net.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Azure Key Vault helps safeguard cryptographic keys and secrets used by cloud app
2727
## Prerequisites
2828

2929
* 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).
3131
* [Azure CLI](/cli/azure/install-azure-cli?view=azure-cli-latest) or [Azure PowerShell](/powershell/azure/overview)
3232

3333
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

Comments
 (0)