gpt4 book ai didi

c# - Azure - 以编程方式创建存储帐户

转载 作者:行者123 更新时间:2023-12-02 06:20:48 27 4
gpt4 key购买 nike

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

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

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: The server failed to authenticate the request. Verify that the certificate is valid and is associated with this subscription.

我不确定这是否是那些误导性消息之一,或者我是否在 Azure 中错误配置了某些内容?

最佳答案

据我了解,Azure现在提供了两种类型的存储管理库。

微软.Azure.管理.存储
Microsoft.WindowsAzure.管理.存储

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

关于c# - Azure - 以编程方式创建存储帐户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45984288/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com