Home > Net >  Microsoft.Azure.OperationalInsights returns 'Forbidden' message for service principle
Microsoft.Azure.OperationalInsights returns 'Forbidden' message for service principle

Time:12-17

I am trying to read insights of a resource in azure. I am getting the error as:

Operation returned an invalid status code 'Forbidden'

The service principle has been given 'Log Analytics Reader' role on the subscription of the workspace.

Microsoft.Azure.Management.ResourceManager.Fluent.Authentication.AzureCredentials credentials =
 Microsoft.Azure.Management.ResourceManager.Fluent.SdkContext.AzureCredentialsFactory.FromServicePrincipal(
 "clientId", 
 "clientSecret", 
 "tenantId", 
 Microsoft.Azure.Management.ResourceManager.Fluent.AzureEnvironment.AzureGlobalCloud);

var client = new Microsoft.Azure.OperationalInsights.OperationalInsightsDataClient(credentials);
client.WorkspaceId = @"workspaceId";
var results = client.QueryWithHttpMessagesAsync("union * | take 5").Result;

The solution to enter image description here

  • And Add the below permission to your service principle.

enter image description here

  • And grant admin consent permission to your service principle for the API permission.

enter image description here

once you have granted the permission for service principle you would be able to read the logs .

For more information on the same thing using powershell please refer this Blog .

  • Related