快速入門:使用 Azure CLI 從 Azure Key Vault 設定及擷取祕密Quickstart: Set and retrieve a secret from Azure Key Vault using Azure CLI

在本快速入門中,您會在 Azure Key Vault 中使用 Azure CLI 建立金鑰保存庫。In this quickstart, you create a key vault in Azure Key Vault with Azure CLI. Azure Key Vault 是一項雲端服務,可作為安全的祕密存放區。Azure Key Vault is a cloud service that works as a secure secrets store. 您也可以安全地儲存金鑰、密碼、憑證和其他祕密。You can securely store keys, passwords, certificates, and other secrets. 如需 Key Vault 的詳細資訊,您可以檢閱概觀For more information on Key Vault you may review the Overview. Azure CLI 可供使用命令列或指令碼來建立和管理 Azure 資源。Azure CLI is used to create and manage Azure resources using commands or scripts. 一旦完成該作業,您就會儲存祕密。Once that you have completed that, you will store a secret.

如果您沒有 Azure 訂用帳戶,請在開始前建立免費帳戶If you don't have an Azure subscription, create a free account before you begin.

使用 Azure Cloud ShellUse Azure Cloud Shell

Azure Cloud Shell 是裝載於 Azure 中的互動式殼層環境,可在瀏覽器中使用。Azure hosts Azure Cloud Shell, an interactive shell environment that you can use through your browser. 您可以使用 Bash 或 PowerShell 搭配 Cloud Shell,與 Azure 服務共同使用。You can use either bash or PowerShell with Cloud Shell to work with Azure services. Azure Cloud Shell 已預先安裝一些命令,可讓您執行本文提到的程式碼,而不必在本機環境上安裝任何工具。You can use the Cloud Shell preinstalled commands to run the code in this article without having to install anything on your local environment.

要啟動 Azure Cloud Shell:To start Azure Cloud Shell:

選項Option 範例/連結Example/Link
選取程式碼區塊右上角的 [試試看] 。Select Try It in the upper-right corner of a code block. 選取 [試用] 並不會自動將程式碼複製到 Cloud Shell 中。Selecting Try It doesn't automatically copy the code to Cloud Shell. Azure Cloud Shell 的試試看範例
請前往 https://shell.azure.com 或選取 [啟動 Cloud Shell] 按鈕,在瀏覽器中開啟 Cloud Shell。Go to https://shell.azure.com or select the Launch Cloud Shell button to open Cloud Shell in your browser. 在新視窗中啟動 Cloud ShellLaunch Cloud Shell in a new window
選取 Azure 入口網站右上角功能表列中的 [Cloud Shell] 按鈕。Select the Cloud Shell button on the top-right menu bar in the Azure portal. Azure 入口網站中的 [Cloud Shell] 按鈕

若要在 Azure Cloud Shell 中執行本文中的程式碼:To run the code in this article in Azure Cloud Shell:

  1. 啟動 Cloud Shell。Start Cloud Shell.

  2. 選取程式碼區塊上的 [複製] 按鈕,複製程式碼。Select the Copy button on a code block to copy the code.

  3. 在 Windows 和 Linux 上選取 Ctrl+Shift+V;或在 macOS 上選取 Cmd+Shift+V,將程式碼貼到 Cloud Shell 工作階段中。Paste the code into the Cloud Shell session by selecting Ctrl+Shift+V on Windows and Linux or by selecting Cmd+Shift+V on macOS.

  4. 選取 Enter 鍵執行程式碼。Select Enter to run the code.

如果您選擇在本機安裝和使用 CLI,本快速入門需要有 Azure CLI 2.0.4 版或更新版本。If you choose to install and use the CLI locally, this quickstart requires the Azure CLI version 2.0.4 or later. 執行 az --version 以尋找版本。Run az --version to find the version. 如果您需要安裝或升級,請參閱安裝 Azure CLIIf you need to install or upgrade, see Install the Azure CLI.

若要使用 CLI 登入 Azure,您可以輸入:To sign in to Azure using the CLI you can type:

az login

若要進一步了解透過 CLI 的登入選項,請參閱使用 Azure CLI 進行登入For more information on login options via the CLI take a look at sign in with Azure CLI

建立資源群組Create a resource group

資源群組是在其中部署與管理 Azure 資源的邏輯容器。A resource group is a logical container into which Azure resources are deployed and managed. 下列範例會在 eastus 位置建立名為 ContosoResourceGroup 的資源群組。The following example creates a resource group named ContosoResourceGroup in the eastus location.

az group create --name "ContosoResourceGroup" --location eastus

建立金鑰保存庫Create a Key Vault

接下來,您會在上一個步驟中建立的資源群組中建立 Key Vault。Next you will create a Key Vault in the resource group created in the previous step. 您必須提供一些資訊:You will need to provide some information:

  • 在本快速入門中,我們使用 Contoso-vault2For this quickstart we use Contoso-vault2. 您必須在測試中提供唯一的名稱。You must provide a unique name in your testing.
  • 資源群組名稱:ContosoResourceGroupResource group name ContosoResourceGroup.
  • 位置:美國東部The location East US.
az keyvault create --name "Contoso-Vault2" --resource-group "ContosoResourceGroup" --location eastus

此 Cmdlet 的輸出會顯示新建立 Key Vault 的屬性。The output of this cmdlet shows properties of the newly created Key Vault. 請記下下列兩個屬性:Take note of the two properties listed below:

  • 保存庫名稱:在此範例中是 Contoso-Vault2Vault Name: In the example, this is Contoso-Vault2. 您將在其他 Key Vault 命令中使用此名稱。You will use this name for other Key Vault commands.
  • 保存庫 URI:在此範例中是 https://contoso-vault2.vault.azure.net/Vault URI: In the example, this is https://contoso-vault2.vault.azure.net/. 透過其 REST API 使用保存庫的應用程式必須使用此 URI。Applications that use your vault through its REST API must use this URI.

此時,您的 Azure 帳戶是唯一獲得授權在此新保存庫上執行任何作業的帳戶。At this point, your Azure account is the only one authorized to perform any operations on this new vault.

將祕密新增至 Key VaultAdd a secret to Key Vault

若要將祕密新增至保存庫,您只需要採取一些額外步驟。To add a secret to the vault, you just need to take a couple of additional steps. 應用程式可以使用此密碼。This password could be used by an application. 此密碼會稱為 ExamplePassword,且其中會儲存 hVFkk965BuUv 值。The password will be called ExamplePassword and will store the value of hVFkk965BuUv in it.

輸入下列命令,以在 Key Vault 中建立稱為 ExamplePassword 並將儲存 hVFkk965BuUv 值的祕密:Type the commands below to create a secret in Key Vault called ExamplePassword that will store the value hVFkk965BuUv :

az keyvault secret set --vault-name "Contoso-Vault2" --name "ExamplePassword" --value "hVFkk965BuUv"

透過使用其 URI,您現在可以參照您新增至 Azure Key Vault 的密碼。You can now reference this password that you added to Azure Key Vault by using its URI. 使用 https://ContosoVault.vault.azure.net/secrets/ExamplePassword 來取得最新版本。Use https://ContosoVault.vault.azure.net/secrets/ExamplePassword to get the current version.

若要以純文字檢視包含在祕密中的值:To view the value contained in the secret as plain text:

az keyvault secret show --name "ExamplePassword" --vault-name "Contoso-Vault2"

現在,您已建立 Key Vault,儲存祕密,並擷取它。Now, you have created a Key Vault, stored a secret, and retrieved it.

清除資源Clean up resources

此集合中的其他快速入門和教學課程會以本快速入門為基礎。Other quickstarts and tutorials in this collection build upon this quickstart. 如果您打算繼續進行後續的快速入門和教學課程,您可以讓這些資源留在原處。If you plan to continue on to work with subsequent quickstarts and tutorials, you may wish to leave these resources in place. 若不再需要,您可以使用 az group delete 命令來移除資源群組和所有相關資源。When no longer needed, you can use the az group delete command to remove the resource group, and all related resources. 您可以刪除資源,如下所示:You can delete the resources as follows:

az group delete --name ContosoResourceGroup

後續步驟Next steps

在本快速入門中,您已建立 Key Vault 並在其中儲存祕密。In this quickstart you created a Key Vault and stored a secret in it. 若要深入了解 Key Vault 以及要如何將其與應用程式整合,請繼續閱讀下列文章。To learn more about Key Vault and how to integrate it with your applications, continue on to the articles below.