2017-08-31 107 views
0

我曾尝试下面的代码来创建在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: The server failed to authenticate the request. Verify that the certificate is valid and is associated with this subscription.

我不知道这是否是那些误导性消息中的一个,或者如果我错误地配置在Azure中的东西吗?

+1

请看看这个回答你的问题:https://stackoverflow.com/questions/35190866/error-making-azure-management-library-api-call-when-authenticating-with-azure-ac/ 35194706#35194706。 –

+0

@GauravMantri非常感谢 - 该链接提供了获取正确标识符的见解 –

回答

1

据我所知,azure现在提供了两种类型的存储管理库。

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

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

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

像老板一样!谢谢......我没有意识到我正在使用“错误的”存储管理库。奇迹般有效! –