允許應用程式存取 Azure Stack Hub Key Vault 祕密
本文中的步驟說明如何執行範例應用程式 HelloKeyVault,以從 Azure Stack Hub 中的金鑰保存庫擷取金鑰和密碼。
必要條件
您可以從 Azure Stack 開發套件安裝下列必要項目,或從 Windows 型外部用戶端 (如果您透過 VPN 連線) 安裝:
建立金鑰保存庫並註冊應用程式
若要準備範例應用程式:
- 在 Azure Stack Hub 中建立金鑰保存庫。
- 在Microsoft Entra識別碼中註冊應用程式。
使用 Azure 入口網站或 PowerShell 來準備範例應用程式。
注意
依預設,PowerShell 指令碼會在 Active Directory 中建立新的應用程式。 不過,您可以註冊其中一個現有的應用程式。
在執行下列指令碼之前,請確實提供 aadTenantName
和 applicationPassword
變數的值。 若未指定 applicationPassword
的值,此指令碼將會產生隨機密碼。
$vaultName = 'myVault'
$resourceGroupName = 'myResourceGroup'
$applicationName = 'myApp'
$location = 'local'
# Password for the application. If not specified, this script generates a random password during app creation.
$applicationPassword = ''
# Function to generate a random password for the application.
Function GenerateSymmetricKey()
{
$key = New-Object byte[](32)
$rng = [System.Security.Cryptography.RNGCryptoServiceProvider]::Create()
$rng.GetBytes($key)
return [System.Convert]::ToBase64String($key)
}
Write-Host 'Please log into your Azure Stack Hub user environment' -foregroundcolor Green
$tenantARM = "https://management.local.azurestack.external"
$aadTenantName = "FILL THIS IN WITH YOUR AAD TENANT NAME. FOR EXAMPLE: myazurestack.onmicrosoft.com"
# Configure the Azure Stack Hub operator's PowerShell environment.
Add-AzEnvironment `
-Name "AzureStackUser" `
-ArmEndpoint $tenantARM
$TenantID = Get-AzsDirectoryTenantId `
-AADTenantName $aadTenantName `
-EnvironmentName AzureStackUser
# Sign in to the user portal.
Connect-AzAccount `
-EnvironmentName "AzureStackUser" `
-TenantId $TenantID `
$now = [System.DateTime]::Now
$oneYearFromNow = $now.AddYears(1)
$applicationPassword = GenerateSymmetricKey
# Create a new Azure AD application.
$identifierUri = [string]::Format("http://localhost:8080/{0}",[Guid]::NewGuid().ToString("N"))
$homePage = "https://contoso.com"
Write-Host "Creating a new AAD Application"
$ADApp = New-AzADApplication `
-DisplayName $applicationName `
-HomePage $homePage `
-IdentifierUris $identifierUri `
-StartDate $now `
-EndDate $oneYearFromNow `
-Password $applicationPassword
Write-Host "Creating a new AAD service principal"
$servicePrincipal = New-AzADServicePrincipal `
-ApplicationId $ADApp.ApplicationId
# Create a new resource group and a key vault in that resource group.
New-AzResourceGroup `
-Name $resourceGroupName `
-Location $location
Write-Host "Creating vault $vaultName"
$vault = New-AzKeyVault -VaultName $vaultName `
-ResourceGroupName $resourceGroupName `
-Sku standard `
-Location $location
# Specify full privileges to the vault for the application.
Write-Host "Setting access policy"
Set-AzKeyVaultAccessPolicy -VaultName $vaultName `
-ObjectId $servicePrincipal.Id `
-PermissionsToKeys all `
-PermissionsToSecrets all
Write-Host "Paste the following settings into the app.config file for the HelloKeyVault project:"
'<add key="VaultUrl" value="' + $vault.VaultUri + '"/>'
'<add key="AuthClientId" value="' + $servicePrincipal.ApplicationId + '"/>'
'<add key="AuthClientSecret" value="' + $applicationPassword + '"/>'
Write-Host
下圖顯示用來建立金鑰保存庫之指令碼的輸出:
記下先前指令碼所傳回 VaultUrl、AuthClientId 和 AuthClientSecret 的值。 您將使用這些值來執行 HelloKeyVault 應用程式。
下載並設定範例應用程式
下載 Azure 金鑰保存庫用戶端範例頁面所提供的金鑰保存庫範例。 將 .zip 檔的內容解壓縮至您的開發工作站。 範例資料夾中有兩個應用程式,本文使用 HelloKeyVault。
若要載入 HelloKeyVault 範例:
- 瀏覽至 Microsoft.Azure.KeyVault.Samples>samples>HelloKeyVault 資料夾。
- 在 Visual Studio 中開啟 HelloKeyVault 應用程式。
設定範例應用程式
在 Visual Studio 中:
開啟 HelloKeyVault\App.config 檔案,然後找出
<appSettings>
元素。以建立金鑰保存庫時傳回的值更新 VaultUrl、AuthClientId 和 AuthCertThumbprint 金鑰。 依預設,App.config 檔案會有
AuthCertThumbprint
的預留位置。 請將此預留位置取代為AuthClientSecret
。<appSettings> <!-- Update these settings for your test environment --> <add key="VaultUrl" value="URL to your Vault" /> <add key="AuthClientId" value="Client Id of your Service Principal" /> <add key="AuthCertThumbprint" value="Thumbprint of the certificate used for authentication" /> <add key="TracingEnabled" value="false" /> </appSettings>
重建方案。
執行應用程式
當您執行HelloKeyVault時,應用程式會登入Microsoft Entra識別碼,然後使用 AuthClientSecret
權杖向 Azure Stack Hub 中的金鑰保存庫進行驗證。
您可以使用 HelloKeyVault 範例來:
- 執行基本作業,例如,對金鑰和密碼執行建立、加密、包裝和刪除等作業。
- 將
encrypt
和decrypt
之類的參數傳至 HelloKeyVault,並將指定的變更套用至金鑰保存庫。