I have an ASP.NET app where I'm trying to connect to the Azure Monitoring API, but keep getting the following exception. I tried to follow a powershell tutorial on how to add a role to my app's service principal (or something), but nothing seemed to work, and I'm not sure if I even need to do it:
ForbiddenError: The server failed to authenticate the request. Verify that the certificate is valid and is associated with this subscription.
Code:
var cred = new ClientCredential(clientId, secret);
var context = new Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext(string.Format(
login,
tenantId));
var task = context
.AcquireTokenAsync(apiEndpoint, cred);
task.Wait();
var result = task.Result;
var subCreds = new Microsoft.Azure.TokenCloudCredentials(subscriptionId, result.AccessToken); //good AccessToken at this point
var alertsClient = new Microsoft.WindowsAzure.Management.Monitoring.Alerts.AlertsClient(subCreds);
var t = c.Incidents.ListActiveForSubscriptionAsync(new System.Threading.CancellationToken());
t.Wait(); //exception at this point