Nota
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare ad accedere o modificare le directory.
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare a modificare le directory.
Azure 密钥保管库有助于保护应用程序、服务和资源所需的机密,例如 API 密钥和数据库连接字符串。
在本教程中,将控制台应用程序配置为从Azure 密钥保管库读取信息。 该应用程序使用 VM 的托管标识向 密钥保管库 进行身份验证。
本教程介绍如何:
- 创建资源组。
- 创建密钥保管库。
- 将机密信息添加到密钥库。
- 从密钥保管库检索机密。
- 创建一个 Azure 虚拟机。
- 为虚拟机启用托管标识。
- 为 VM 标识分配权限。
在开始之前,请阅读 密钥保管库 的基本概念。
如果没有 Azure 订阅,请创建一个试用版版订阅。
先决条件
对于 Windows、Mac 和 Linux:
创建资源并分配权限
在开始编码之前,需要创建一些资源,将机密放入密钥保管库,并分配权限。
登录 Azure
使用Azure CLI或Azure PowerShell登录到Azure:
az login
创建资源组和密钥保管库
本快速入门使用预先创建的 Azure 密钥保管库。 可以按照以下快速入门中的步骤创建密钥保管库:
或者,也可运行这些 Azure CLI 或 Azure PowerShell 命令。
重要
每个密钥保管库必须具有唯一的名称。 在以下示例中,将 <vault-name> 替换为您的密钥保管库的名称。
az group create --name "myResourceGroup" -l "chinaeast2"
az keyvault create --name "<vault-name>" -g "myResourceGroup" --enable-rbac-authorization true
使用机密填充密钥保管库
让我们创建一个名为 mySecret 的机密,其值为 Success!。 机密可以是密码、SQL 连接字符串,或者需要安全保存的、可供应用程序使用的其他任何信息。
若要将机密添加到新创建的密钥保管库,请使用以下命令:
az keyvault secret set --vault-name "<vault-name>" --name "mySecret" --value "Success!"
创建虚拟机
使用以下方法之一创建Windows或 Linux VM:
| Windows操作系统 | Linux |
|---|---|
| Azure CLI | Azure CLI |
| PowerShell | PowerShell |
| Azure 门户 | Azure 门户 |
为 VM 分配标识
为 VM 创建系统分配的托管标识:
az vm identity assign --name <vm-name> --resource-group <resource-group>
记下以下代码中显示的系统分配的标识。 以上命令的输出为:
{
"systemAssignedIdentity": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"userAssignedIdentities": {}
}
为 VM 标识分配权限
若要通过 Role-Based 访问控制(RBAC)获取密钥保管库的权限,请使用 Azure CLI 命令 az role assignment create 将角色分配给“用户主体名称”(UPN)。
az role assignment create --role "Key Vault Secrets User" --assignee "<upn>" --scope "/subscriptions/<subscription-id>/resourceGroups/<resource-group-name>/providers/Microsoft.KeyVault/vaults/<your-unique-keyvault-name>"
将 <upn>、<subscription-id>、<resource-group-name> 和 <your-unique-keyvault-name> 替换为你的实际值。 你的 UPN 通常采用电子邮件地址格式(例如 username@domain.com)。
登录到 VM
若要登录到 VM,请按照 Connect 中的说明登录到Azure Windows虚拟机或 Connect 并登录到 Azure Linux 虚拟机。
设置控制台应用
使用命令创建控制台应用并安装所需的包 dotnet 。
安装 .NET
若要安装.NET,请转到.NET下载页。
创建并运行示例 .NET 应用
打开命令提示符并运行:
dotnet new console -n keyvault-console-app
cd keyvault-console-app
dotnet run
应用将“Hello World”打印到控制台。
安装软件包
在控制台窗口中,安装Azure 密钥保管库机密客户端库和Azure标识库:
dotnet add package Azure.Security.KeyVault.Secrets
dotnet add package Azure.Identity
编辑控制台应用
打开 Program.cs 并添加以下 using 指令:
using System;
using Azure.Core;
using Azure.Identity;
using Azure.Security.KeyVault.Secrets;
将 Main 该方法替换为以下代码,并更新 <vault-name> 到密钥保管库名称。 此代码使用 DefaultAzureCredential 对密钥保管库进行身份验证,该密钥保管库使用 VM 托管标识中的令牌。 它还将重试配置为指数退避,以防 密钥保管库 被限流。
class Program
{
static void Main(string[] args)
{
string secretName = "mySecret";
string keyVaultName = "<vault-name>";
var kvUri = "https://<vault-name>.vault.azure.cn";
SecretClientOptions options = new SecretClientOptions()
{
Retry =
{
Delay= TimeSpan.FromSeconds(2),
MaxDelay = TimeSpan.FromSeconds(16),
MaxRetries = 5,
Mode = RetryMode.Exponential
}
};
var client = new SecretClient(new Uri(kvUri), new DefaultAzureCredential(),options);
Console.Write("Input the value of your secret > ");
string secretValue = Console.ReadLine();
Console.Write("Creating a secret in " + keyVaultName + " called '" + secretName + "' with the value '" + secretValue + "' ...");
client.SetSecret(secretName, secretValue);
Console.WriteLine(" done.");
Console.WriteLine("Forgetting your secret.");
secretValue = "";
Console.WriteLine("Your secret is '" + secretValue + "'.");
Console.WriteLine("Retrieving your secret from " + keyVaultName + ".");
KeyVaultSecret secret = client.GetSecret(secretName);
Console.WriteLine("Your secret is '" + secret.Value + "'.");
Console.Write("Deleting your secret from " + keyVaultName + " ...");
client.StartDeleteSecret(secretName);
System.Threading.Thread.Sleep(5000);
Console.WriteLine(" done.");
}
}
清理资源
不再需要它们时,请删除 VM 和密钥保管库。