我正在使用 Microsoft.WindowsAzure.Management 和 Microsoft.IdentityModel.Clients.ActiveDirectory 包尝试从 C# 代码使用 Azure 管理 API,但是当我尝试从中检索一些数据时,我总是收到错误消息:
ForbiddenError: The server failed to authenticate the request. Verify that the certificate is valid and is associated with this subscription.
这是我正在使用的代码示例:
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using Microsoft.WindowsAzure.Management;
var authContext = new Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext("https://login.windows.net/mytenant");
var cc = new Microsoft.IdentityModel.Clients.ActiveDirectory.ClientCredential("azure-app-id", "azure-app-secret");
var token = await authContext.AcquireTokenAsync("https://management.core.windows.net/", cc);
var tokenCred = new Microsoft.Azure.TokenCloudCredentials(token.AccessToken);
var client = new ManagementClient(tokenCred);
// this is where I get the error:
var subscriptions = await client.Subscriptions.GetAsync(CancellationToken.None);