我已尝试使用以下代码在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中配置错误?
答案 0 :(得分:1)
据我所知,azure现在提供两种类型的存储管理库。
Microsoft.Azure.Management.Storage和Microsoft.WindowsAzure.Management.Storage
Microsoft.Azure.Management.Storage用于创建新的arm存储
Microsoft.WindowsAzure.Management.Storage用于创建经典的asm存储
我想你想要创建新的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;
}