快速入门:使用 Azure CLI 在 Azure Key Vault 中设置和检索密钥Quickstart: Set and retrieve a key from Azure Key Vault using Azure CLI

在本快速入门中,你将使用 Azure CLI 在 Azure Key Vault 中创建一个密钥保管库。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 key.

如果没有 Azure 订阅,可在开始前创建一个试用帐户If you don't have an Azure subscription, create a trial account before you begin.

如果选择在本地安装并使用 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 cloud set -n AzureChinaCloud
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. 以下示例在“chinanorth” 位置创建名为“ContosoResourceGroup” 的资源组。The following example creates a resource group named ContosoResourceGroup in the chinanorth location.

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

创建密钥保管库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 China North.
az keyvault create --name "Contoso-Vault2" --resource-group "ContosoResourceGroup" --location chinanorth

此 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.cn/Vault URI: In the example, this is https://contoso-vault2.vault.azure.cn/. 通过其 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 Vault 添加密钥Add a key to Key Vault

只需再执行几个步骤便可向保管库中添加密钥。To add a key to the vault, you just need to take a couple of additional steps. 此密钥可供应用程序使用。This key could be used by an application.

键入以下命令,创建名为 ExampleKey 的密钥:Type the commands below to create a called ExampleKey :

az keyvault key create --vault-name "Contoso-Vault2" -n ExampleKey --protection software

现在,可以通过 URI 来引用已添加到 Azure Key Vault 的此密钥。You can now reference this key that you added to Azure Key Vault by using its URI. 使用 https://Contoso-Vault2.vault.azure.cn/keys/ExampleKey 获取当前版本。Use https://Contoso-Vault2.vault.azure.cn/keys/ExampleKey to get the current version.

若要查看以前存储的密钥,请使用以下命令:To view previously stored key:


az keyvault key show --name "ExampleKey" --vault-name "Contoso-Vault2"

现在,你已创建了一个密钥保管库,存储了一个密钥并检索了该密钥。Now, you have created a Key Vault, stored a key, 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 key in it. 若要详细了解 Key Vault 以及如何将其与应用程序集成,请继续阅读以下文章。To learn more about Key Vault and how to integrate it with your applications, continue on to the articles below.