是否可以使用 C# 重新生成存储帐户密钥?

Is it possible to regenerate Storage account keys using C#?

我想使用 C# 代码重新生成存储帐户密钥,并用重新生成的密钥替换密钥保管库中的旧密钥。请帮助我。

我猜你必须选择 Azure Rest API Reference

Here's link 描述重新生成给定存储帐户的密钥的调用。

使用 Microsoft.Azure.Management.FluentMicrosoft.Azure.Management.ResourceManager.Fluent 包,下面是我重新生成密钥的代码。

        string clientSecret = "client secret";
        string clientId = "client id";

        var azureCredentials = new AzureCredentials(new ServicePrincipalLoginInformation
        {
            ClientId = clientId,
            ClientSecret = clientSecret
        }, tenantId, AzureEnvironment.AzureGlobalCloud);

        var azure = Azure
        .Configure()
        .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
        .Authenticate(azureCredentials)
        .WithDefaultSubscription();

        var keyName = "key1";

        var storageAccount = azure.StorageAccounts.GetByResourceGroup("resource group name", "storage account name");

        var key = storageAccount.RegenerateKey(keyName);
        Console.WriteLine(key.FirstOrDefault()?.Value);