我正在尝试从代码(尤其是从Azure功能应用程序中的C#代码)获取和重置Azure Cosmos DB帐户的主密钥,该功能的系统分配的托管身份在Cosmos DB帐户上定义了RBAC角色。 Cosmos DB客户端api似乎没有此功能。
答案 0 :(得分:0)
这是一个有关如何从github存储库生成master key
的示例。
private static string GenerateMasterKeyAuthorizationSignature(string verb, string resourceId, string resourceType, string key, string keyType, string tokenVersion)
{
var hmacSha256 = new System.Security.Cryptography.HMACSHA256 { Key = Convert.FromBase64String(key) };
string payLoad = string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0}\n{1}\n{2}\n{3}\n{4}\n",
verb.ToLowerInvariant(),
resourceType.ToLowerInvariant(),
resourceId,
utc_date.ToLowerInvariant(),
""
);
byte[] hashPayLoad = hmacSha256.ComputeHash(System.Text.Encoding.UTF8.GetBytes(payLoad));
string signature = Convert.ToBase64String(hashPayLoad);
return System.Web.HttpUtility.UrlEncode(String.Format(System.Globalization.CultureInfo.InvariantCulture, "type={0}&ver={1}&sig={2}",
keyType,
tokenVersion,
signature));
}
答案 1 :(得分:0)
您可以使用Azure Management Fluent API进行此操作。 您需要包含此nuget程序包“ Microsoft.Azure.Management.Fluent ”。 这是link
下面是相同的示例代码。
var credentials = SdkContext.AzureCredentialsFactory
.FromServicePrincipal(clientId,
clientSecret,
tenantId,
AzureEnvironment.AzureGlobalCloud);
IAzure azure = Azure.Authenticate(credentials).WithSubscription("<<Your subscription Id>>");
var cosmosaccount = azure.CosmosDBAccounts.GetByResourceGroup("<<Your cosmos account resource group name>>", "<<Your cosmos account name>>");
Console.WriteLine(cosmosaccount.ListKeys().SecondaryMasterKey);
cosmosaccount.RegenerateKey("secondary");
Console.WriteLine(cosmosaccount.ListKeys().SecondaryMasterKey);