0

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
MStodd
  • 4,716
  • 3
  • 30
  • 50
  • 2
    Please see this thread for more information on why you're receiving this error: http://stackoverflow.com/questions/35190866/error-making-azure-management-library-api-call-when-authenticating-with-azure-ac. – Gaurav Mantri Nov 03 '16 at 17:27
  • @GauravMantri that worked perfectly. Having spent so long on this trying to get the official azure nuget package to work, I have such mixed emotions right now – MStodd Nov 04 '16 at 02:41

0 Answers0