Azure 管理 API を使用してストレージ アカウントにアクセスする 認証には TokenCloudCredentials を使用します。これは、Resource Management のストレージ アカウントでは正常に機能しますが、クラシック ストレージ アカウントでは機能しません。
TokenCloudCredentials を使用して従来のストレージ アカウント クライアントでメソッドを実行しようとすると、次のエラー メッセージが表示されました。
ForbiddenError: サーバーは要求を認証できませんでした。証明書が有効で、このサブスクリプションに関連付けられていることを確認してください。
コード - Azure Resource Manager の作業方法
var clientId = "{clientID}";
var tenant = "{tenant GUID}";
var pw = "{password}";
var authenticationContext = new AuthenticationContext("https://login.windows.net/" + tenant);
var credential = new ClientCredential(clientId , pw);
Task<AuthenticationResult> tskGetToken = authenticationContext.AcquireTokenAsync(resource: "https://management.core.windows.net/", credential);
AuthenticationResult token = tskGetToken.Result;
SubscriptionCloudCredentials creds = new TokenCloudCredentials("{subscription id}", token.AccessToken);
StorageManagementClient smc = new StorageManagementClient(creds);
Task<StorageAccountListKeysResponse> tskTargetKeysSource = smc.StorageAccounts.ListKeysAsync("{resource group}", "{storage account name");
while (tskTargetKeysSource.Status != TaskStatus.RanToCompletion)
{
if (tskTargetKeysSource.Exception != null)
throw tskTargetKeysSource.Exception;
Console.WriteLine("Running - Getting target storage account Storage Key");
Thread.Sleep(2500);
}
これは機能し、ストレージ キーを受け取りました。
Azure クラシック ストレージの壊れたメソッド:
var clientId = "{clientID}";
var tenant = "{tenant GUID}";
var pw = "{password}";
var authenticationContext = new AuthenticationContext("https://login.windows.net/" + tenant);
var credential = new ClientCredential(clientId , pw);
Task<AuthenticationResult> tskGetToken = authenticationContext.AcquireTokenAsync(resource: "https://management.core.windows.net/", credential);
AuthenticationResult token = tskGetToken.Result;
SubscriptionCloudCredentials creds = new TokenCloudCredentials("{subscription id}", token.AccessToken);
Microsoft.WindowsAzure.Management.Storage.StorageManagementClient classicSmc = new Microsoft.WindowsAzure.Management.Storage.StorageManagementClient(creds);
Task<StorageAccountGetKeysResponse> tskSourceKeysSource = classicSmc.StorageAccounts.GetKeysAsync("{storage account name}", new CancellationToken());
while (tskSourceKeysSource.Status != TaskStatus.RanToCompletion)
{
if (tskSourceKeysSource.Exception != null)
throw tskSourceKeysSource.Exception; // Exception thrown here
Console.WriteLine("Running - Getting source storage account Storage Key");
Thread.Sleep(2500);
}
違いがわかりません。私が書いているアプリケーションには、Azure Active Directory で適切なアクセス許可があり、適切なリソース (ストレージ アカウント、リソース グループなど) に対するアクセス許可があります。これらの操作も同じサブスクリプションを使用しています。