本文介绍了以编程方式获取/重置Azure Cosmos DB主密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从代码(尤其是从Azure功能应用程序中的C#代码)获取和重置Azure Cosmos DB帐户的主密钥,该功能的系统分配的受管理身份具有在Cosmos DB帐户上定义的RBAC角色.Cosmos DB客户端api似乎没有此功能.

I am trying to get and reset the master keys for an Azure Cosmos DB account from code, specifically from C# code in an Azure Function App whose system assigned managed identity has an RBAC role defined on the Cosmos DB account. The Cosmos DB client api does not appear to have this functionality.

推荐答案

您可以使用Azure Management Fluent API进行此操作.您需要包含此nuget程序包" Microsoft.Azure.Management.Fluent ".这是链接

You can do this using Azure Management Fluent API.You need to include this nuget package "Microsoft.Azure.Management.Fluent".Here is the link

下面是相同的示例代码.

Below is the sample code for the same.

    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);

这篇关于以编程方式获取/重置Azure Cosmos DB主密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-02 01:51