0

我正在编写一个 .Net 控制台应用程序来创建 Key Vault,但无法在 Microsoft.Azure.KeyVault 程序集中找到允许创建 Vault 并将服务主体设置到该 Vault 的类/方法。

有人可以指出我可以用来创建保险库的程序集/类吗?

谢谢

4

2 回答 2

2

您要查找的类是 Microsoft.Azure 中的KeyVaultManagementClient管理.KeyVault 命名空间。这是在您可以从 NuGet 获得的管理 KeyVault 程序集中定义的。

在此处输入图像描述

执行此操作的代码的主要部分如下所示。但是,请注意,我已经缩写了一些您必须进一步定义和初始化的内容(属性、订阅凭据等)。如果您想查看完整的解决方案,请查看 . NET Azure SDK,特别是KeyVaultManagement.Tests项目。

        // The resource group to create the vault in.
        const string resourceGroupName = "Vaults-Resource-Group";

        // The name of the vault to create.
        const string vaultName = "web-app-01-vault";

        // Define access policies to keys and secrets (abbreviated just to illustrate...)
        var accessPolicy = new AccessPolicyEntry
        {
            ApplicationId = sp, 
            PermissionsToKeys = new string[] { "all" }, 
            PermissionsToSecrets = new string[] { "backup", "create", "delete" } //etc.  just to name a few
        };

        // Define vault properties (abbreviated just to illustrate...)
        VaultProperties vaultProps = new VaultProperties()
        {
            EnabledForTemplateDeployment = true,
            AccessPolicies = new List<AccessPolicyEntry>()
            {
                accessPolicy
            }
        };

        // Initialize 'create parameters' to create the vault in "West US"
        VaultCreateOrUpdateParameters vaultParams = new VaultCreateOrUpdateParameters(vaultProps, "westus");

        // Initialize an instance to the mgmt client
        // NOTE: Need to initialize creds derived from SubscriptionCloudCredentials
        KeyVaultManagementClient mgmtClient = new KeyVaultManagementClient(creds);

        // Create the vault
        mgmtClient.Vaults.CreateOrUpdateAsync(resourceGroupName, vaultName, vaultParams);
于 2016-05-17T17:30:35.687 回答
0

出于某种原因,客户端 SDK 中没有这样的功能(或者,至少,即使通过 SDK 的 Github 存储库中放置的代码,我也无法找到该功能)。有用于创建/更新密钥保管库的REST API,因此您可以使用它来创建它。或 PowerShell - 可以从 C# 执行 Powershell,我尝试这样做 - 它可以工作,但应该进行测试。

于 2016-05-17T12:57:50.283 回答