Skip to content

Commit d877f7f

Browse files
2 parents f536c3e + 90bca8a commit d877f7f

File tree

2 files changed

+71
-9
lines changed

2 files changed

+71
-9
lines changed

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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,26 @@ This .NET quickstart relies on environment variables to store credentials that s
149149

150150
Before you build and run your app, use the `setx` command to set the `akvClientId`, `akvClientSecret`, `akvTenantId`, and `akvSubscriptionId` environment variables to the values you noted above.
151151

152+
**Windows**
153+
152154
```console
153-
setx akvClientId <your-clientID>
155+
setx akvClientId "<your-clientID>"
156+
setx akvClientSecret "<your-clientSecret>"
157+
```
158+
159+
**Linux**
154160

155-
setx akvClientSecret <your-clientSecret>
156-
````
161+
```bash
162+
export akvClientId = "<your-clientID>"
163+
export akvClientSecret = "<your-clientSecret>"
164+
```
165+
166+
**MacOS**
157167

158-
Each time you call `setx`, you should get a response of "SUCCESS: Specified value was saved."
168+
```bash
169+
export akvClientId = "<your-clientID>"
170+
export akvClientSecret = "<your-clientSecret>"
171+
```
159172

160173
Assign these environment variables to strings in your code, and then authenticate your application by passing them to the [KeyVaultClient class](/dotnet/api/microsoft.azure.keyvault.keyvaultclient):
161174

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

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Quickstart - Azure Key Vault client library for .NET (v4)
33
description: Learn how to create, retrieve, and delete secrets from an Azure key vault using the .NET client library (v4)
44
author: msmbaldwin
55
ms.author: mbaldwin
6-
ms.date: 05/20/2019
6+
ms.date: 03/12/2020
77
ms.service: key-vault
88
ms.subservice: secrets
99
ms.topic: quickstart
@@ -36,7 +36,7 @@ This quickstart assumes you are running `dotnet`, [Azure CLI](/cli/azure/install
3636

3737
### Create new .NET console app
3838

39-
In a console window, use the `dotnet new` command to create a new .NET console app with the name `akv-dotnet`.
39+
In a console window, use the `dotnet new` command to create a new .NET console app with the name `key-vault-console-app`.
4040

4141
```console
4242
dotnet new console -n key-vault-console-app
@@ -61,13 +61,13 @@ Build succeeded.
6161
From the console window, install the Azure Key Vault client library for .NET:
6262

6363
```console
64-
dotnet add package Azure.Security.KeyVault.Secrets --version 4.0.0
64+
dotnet add package Azure.Security.KeyVault.Secrets
6565
```
6666

6767
For this quickstart, you will need to install the following packages as well:
6868

6969
```console
70-
dotnet add package Azure.Identity --version 1.0.0
70+
dotnet add package Azure.Identity
7171
```
7272

7373
### Create a resource group and key vault
@@ -83,6 +83,12 @@ az group create --name "myResourceGroup" -l "EastUS"
8383
az keyvault create --name <your-unique-keyvault-name> -g "myResourceGroup"
8484
```
8585

86+
```azurepowershell
87+
New-AzResourceGroup -Name myResourceGroup -Location EastUS
88+
89+
New-AzKeyVault -Name <your-unique-keyvault-name> -ResourceGroupName myResourceGroup -Location EastUS
90+
```
91+
8692
### Create a service principal
8793

8894
The simplest way to authenticate a cloud-based .NET application is with a managed identity; see [Use an App Service managed identity to access Azure Key Vault](managed-identity.md) for details. For the sake of simplicity however, this quickstart creates a .NET console application. Authenticating a desktop application with Azure requires the use of a service principal and an access control policy.
@@ -109,14 +115,39 @@ This operation will return a series of key / value pairs.
109115
}
110116
```
111117

118+
Create a service principal using Azure PowerShell [New-AzADServicePrincipal](/powershell/module/az.resources/new-azadserviceprincipal) command:
119+
120+
```azurepowershell
121+
# Create a new service principal
122+
$spn = New-AzADServicePrincipal -DisplayName "http://mySP"
123+
124+
# Get the tenant ID and subscription ID of the service principal
125+
$tenantId = (Get-AzContext).Tenant.Id
126+
$subscriptionId = (Get-AzContext).Subscription.Id
127+
128+
# Get the client ID
129+
$clientId = $spn.ApplicationId
130+
131+
# Get the client Secret
132+
$bstr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($spn.Secret)
133+
$clientSecret = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($bstr)
134+
```
135+
136+
For more details about the service principal with Azure PowerShell, refer to [Create an Azure service principal with Azure PowerShell](/powershell/azure/create-azure-service-principal-azureps).
137+
112138
Take note of the clientId, clientSecret, and tenantId, as we will use them in the following steps.
113139

140+
114141
#### Give the service principal access to your key vault
115142

116143
Create an access policy for your key vault that grants permission to your service principal by passing the clientId to the [az keyvault set-policy](/cli/azure/keyvault?view=azure-cli-latest#az-keyvault-set-policy) command. Give the service principal get, list, and set permissions for both keys and secrets.
117144

118145
```azurecli
119-
az keyvault set-policy -n <your-unique-keyvault-name> --spn <clientId-of-your-service-principal> --secret-permissions delete get list set --key-permissions create decrypt delete encrypt get list unwrapKey wrapKey
146+
az keyvault set-policy -n <your-unique-keyvault-name> --spn <clientId-of-your-service-principal> --secret-permissions list get set delete purge
147+
```
148+
149+
```azurepowershell
150+
Set-AzKeyVaultAccessPolicy -VaultName <your-unique-keyvault-name> -ServicePrincipalName <clientId-of-your-service-principal> -PermissionsToSecrets list,get,set,delete,purge
120151
```
121152

122153
#### Set environmental variables
@@ -137,6 +168,16 @@ setx KEY_VAULT_NAME <your-key-vault-name>
137168

138169
Each time you call `setx`, you should get a response of "SUCCESS: Specified value was saved."
139170

171+
```shell
172+
AZURE_CLIENT_ID=<your-clientID>
173+
174+
AZURE_CLIENT_SECRET=<your-clientSecret>
175+
176+
AZURE_TENANT_ID=<your-tenantId>
177+
178+
KEY_VAULT_NAME=<your-key-vault-name>
179+
```
180+
140181
## Object model
141182

142183
The Azure Key Vault client library for .NET allows you to manage keys and related assets such as certificates and secrets. The code samples below will show you how to create a client, set a secret, retrieve a secret, and delete a secret.
@@ -169,6 +210,10 @@ You can verify that the secret has been set with the [az keyvault secret show](/
169210
az keyvault secret show --vault-name <your-unique-keyvault-name> --name mySecret
170211
```
171212

213+
```azurepowershell
214+
(Get-AzKeyVaultSecret -VaultName <your-unique-keyvault-name> -Name mySecret).SecretValueText
215+
```
216+
172217
### Retrieve a secret
173218

174219
You can now retrieve the previously set value with the [client.GetSecret method](/dotnet/api/microsoft.azure.keyvault.keyvaultclientextensions.getsecretasync).
@@ -189,6 +234,10 @@ You can verify that the secret is gone with the [az keyvault secret show](/cli/a
189234
az keyvault secret show --vault-name <your-unique-keyvault-name> --name mySecret
190235
```
191236

237+
```azurepowershell
238+
(Get-AzKeyVaultSecret -VaultName <your-unique-keyvault-name> -Name mySecret).SecretValueText
239+
```
240+
192241
## Clean up resources
193242

194243
When no longer needed, you can use the Azure CLI or Azure PowerShell to remove your key vault and the corresponding resource group.

0 commit comments

Comments
 (0)