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

问题描述:

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

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

感谢

您正在寻找的课程是Microsoft.Azure中的KeyVaultManagementClient管理 .KeyVault命名空间。这是在您可以从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); 
+0

Thanks @Rick。我可以使用[KeyVaultManagementClient](https://github.com/Azure/azure-sdk-for-net/blob/master/src/ResourceManagement/KeyVaultManagement/KeyVaultManagement.Tests/VaultOperationsTest.cs)创建/更新Vault。 – NKDev

出于某种原因,没有在客户端SDK没有这样的功能(或者,至少,我是不是能够找到以及甚至通过放置的GitHub的回购的代码会SDK)。 创建/更新密钥保险库有REST API,因此您可以使用它创建。或PowerShell - 有可能执行Powershell from C#,我试图做到这一点 - 它的工作原理,但应该进行测试。