使用API​​在Azure Key Vault中创建密钥

时间:2018-07-26 10:49:22

标签: azure azure-keyvault

我在指定的订阅中通过创建了一个蔚蓝的密钥库。关注了这篇文章

https://docs.microsoft.com/en-us/rest/api/keyvault/keyvaultpreview/vaults/createorupdate#examples

当api调用时,天蓝色文件库成功创建。现在,我还需要为创建的Key Vault创建一个密钥。在创建天蓝色的密钥保管库时是否可以创建密钥?

1 个答案:

答案 0 :(得分:1)

  

创建天蓝色的密钥库时是否可以创建密钥?

正如juunas所说,您需要单独拨打电话才能实现自己想要的目标。

我用以下代码对其进行了测试,它可以正常工作。 resourceUri是https://vault.azure.net。有关更多详细信息,请参阅此SO thread

Key vault频道中,您需要Add policies至注册的应用程序或用户。在Access Control中,您需要add permission到注册的应用程序或用户。

enter image description here enter image description here

var appId = "0000000000000000000000000000000";
var secretKey = "******************************************";
var tenantId = "0000000000000000000000000000000";
var context = new AuthenticationContext("https://login.windows.net/" + tenantId);
ClientCredential clientCredential = new ClientCredential(appId, secretKey);
var tokenResponse = context.AcquireTokenAsync("https://vault.azure.net", clientCredential).Result;
var accessToken = tokenResponse.AccessToken;
using (var client = new HttpClient())
{
    client.DefaultRequestHeaders.Add("Authorization", "Bearer " + accessToken);
    var requestURl = "https://xxxxxx.vault.azure.net/keys/xxxx/create?api-version=2016-10-01";
    string body = "{\"kty\": \"RSA\"}";
    var stringContent = new StringContent(body, Encoding.UTF8, "application/json");
    var response = client.PostAsync(requestURl, stringContent).Result;
}

enter image description here