2017-08-31 148 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

像老闆一樣!謝謝......我沒有意識到我正在使用「錯誤的」存儲管理庫。奇蹟般有效! –