使用.NET程序集创建Azure密钥保管库(Microsoft.Azure.KeyVault)

时间:2016-05-17 12:38:29

标签: azure azure-keyvault

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

有人可以指点我可以用来创建金库的汇编/类。

由于

2 个答案:

答案 0 :(得分:2)

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

enter image description here

执行此操作的代码的主要部分如下所示。但是,请注意我已经缩写了一些您必须进一步定义和初始化的东西(属性,订阅凭据等)。如果您想查看完整的解决方案,请查看。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);

答案 1 :(得分:0)

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