【问题标题】:Azure - Programmatically Create Storage AccountAzure - 以编程方式创建存储帐户
【发布时间】:2018-02-09 13:51:17
【问题描述】:

我已尝试使用以下代码在 Azure 中创建新的存储帐户:

获取令牌(成功 - 我收到了令牌):

var cc = new ClientCredential("clientId", "clientSecret");
var context = new AuthenticationContext("https://login.windows.net/subscription");
var result = context.AcquireTokenAsync("https://management.azure.com/", cc);

创建云存储凭据:

var credential = new TokenCloudCredentials("subscription", token);

创建云存储帐户(失败):

using (var storageClient = new StorageManagementClient(credentials))
{
    await storageClient.StorageAccounts.CreateAsync(new StorageAccountCreateParameters
    {
        Label = "samplestorageaccount",
        Location = LocationNames.NorthEurope,
        Name = "myteststorage",
        AccountType = "RA-GRS"
    });
}

错误:

ForbiddenError:服务器未能验证请求。核实 证书有效并与此相关联 订阅。

我不确定这是误导性消息之一,还是我在 Azure 中配置错误?

【问题讨论】:

标签: c# azure azure-blob-storage


【解决方案1】:

据我所知,Azure 目前提供两种存储管理库。

Microsoft.Azure.Management.Storage
Microsoft.WindowsAzure.Management.Storage

Microsoft.Azure.Management.Storage 用于创建新的 ARM 存储。

Microsoft.WindowsAzure.Management.Storage 用于创建经典 ARM 存储。

我猜您想创建新的 arm 存储,但您使用了“Microsoft.WindowsAzure.Management.Storage”库。由于“Microsoft.WindowsAzure.Management.Storage”使用证书来验证请求,您将收到错误消息。如果你想知道如何使用“Microsoft.WindowsAzure.Management.Storage”来创建经典存储,我建议你参考这个article。

我假设你想创建新的 ARM 存储,我建议你安装“Microsoft.Azure.Management.Storage”Nuget 包。

更多细节,您可以参考以下代码。

    static void Main(string[] args)
    {
        var subscriptionId = "your subscriptionId";
        var clientId = "your client id";
        var tenantId = "your tenantid";
        var secretKey = "secretKey";
        StorageManagementClient StorageManagement = new StorageManagementClient(new Microsoft.Azure.TokenCloudCredentials(subscriptionId, GetAccessToken(tenantId, clientId, secretKey)));
       
        var re= StorageManagement.StorageAccounts.CreateAsync("groupname", "sotrage name",new Microsoft.Azure.Management.Storage.Models.StorageAccountCreateParameters() {
              Location = LocationNames.NorthEurope,
             AccountType = Microsoft.Azure.Management.Storage.Models.AccountType.PremiumLRS
        },new CancellationToken() { }).Result;

        Console.ReadKey();
    }

    static string GetAccessToken(string tenantId, string clientId, string secretKey)
    {
        var authenticationContext = new AuthenticationContext($"https://login.windows.net/{tenantId}");
        var credential = new ClientCredential(clientId, secretKey);
        var result = authenticationContext.AcquireTokenAsync("https://management.core.windows.net/",
            credential);

        if (result == null)
        {
            throw new InvalidOperationException("Failed to obtain the JWT token");
        }

        var token = result.Result.AccessToken;
        return token;
    }

【讨论】:

  • 像个老板!谢谢...我不知道我使用的是“不正确”的存储管理库。像魅力一样工作!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-26
  • 2014-01-26
  • 2017-11-23
  • 2018-01-08
  • 1970-01-01
  • 2011-10-12
相关资源
最近更新 更多