Skip to content

Commit eb74cb9

Browse files
authored
Merge pull request #107588 from justinyoo/kv-quick-net-2
Azure Key Vault Quickstart .NET (v4) part 2
2 parents e43af74 + d66eb0f commit eb74cb9

File tree

1 file changed

+40
-1
lines changed

1 file changed

+40
-1
lines changed

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

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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
@@ -179,6 +210,10 @@ You can verify that the secret has been set with the [az keyvault secret show](/
179210
az keyvault secret show --vault-name <your-unique-keyvault-name> --name mySecret
180211
```
181212

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

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

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

204243
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)