This example shows a C# console application that calls the Analytics API as itself, with no user signing in. It uses the Microsoft Authentication Library (MSAL) or Azure.Identity to get an Entra access token and reads the schedule types. It is for .NET developers, and assumes the application has already been given an Analytics role as described in Setting up API Applications (Using Analytics → Analytics API).
This article was previously called Managed Identity Authority. The original sample used a client secret, not a managed identity. Both are shown below.
What you need
| Value | Where to find it |
|---|---|
| Tenant ID | Microsoft Entra ID → Overview |
| Client ID of your application | Its app registration (not needed for a system-assigned managed identity) |
| Client secret or certificate | Its app registration → Certificates & secrets |
| Analytics API Application ID URI | The Analytics API app registration → Expose an API, normally api://<analytics-api-client-id> |
| Analytics API URL | Your installation, for example https://analytics-api.contoso.com |
The application must have the DataReader role as an Application permission with admin consent, or, for a managed identity, an app role assignment.
Project
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Azure.Identity" Version="1.13.2" />
</ItemGroup>
</Project>
Program.cs
Azure.Identity covers both cases with one line each. Keep the secret out of the code: here it is read from environment variables.
using System.Net.Http.Headers;
using System.Text.Json;
using Azure.Core;
using Azure.Identity;
var analyticsApiUrl = Environment.GetEnvironmentVariable("ANALYTICS_API_URL")!; // https://analytics-api.contoso.com
var analyticsAppIdUri = Environment.GetEnvironmentVariable("ANALYTICS_APP_ID_URI")!; // api://<analytics-api-client-id>
// Running in Azure with a managed identity: no secret at all.
TokenCredential credential = new ManagedIdentityCredential();
// Running elsewhere: a service principal with a client secret (or use ClientCertificateCredential).
// TokenCredential credential = new ClientSecretCredential(
// Environment.GetEnvironmentVariable("TENANT_ID"),
// Environment.GetEnvironmentVariable("CLIENT_ID"),
// Environment.GetEnvironmentVariable("CLIENT_SECRET"));
// Always ask for <Application ID URI>/.default: the token then carries the roles assigned to the application.
var token = await credential.GetTokenAsync(new TokenRequestContext([$"{analyticsAppIdUri}/.default"]), default);
using var http = new HttpClient { BaseAddress = new Uri(analyticsApiUrl) };
http.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.Token);
using var response = await http.GetAsync("/api/ReportScheduleType");
response.EnsureSuccessStatusCode();
using var json = JsonDocument.Parse(await response.Content.ReadAsStreamAsync());
foreach (var type in json.RootElement.EnumerateArray())
{
Console.WriteLine($"{type.GetProperty("id").GetInt32()}: {type.GetProperty("title").GetString()}");
}
Tokens last about an hour. A long-running service should call GetTokenAsync before each request, or at least before expiry. The credential caches the token and only goes to Entra when it needs a new one.
Output
One line per schedule type, for example:
1: Level 1 schedules
2: Level 2 schedules
When it fails
| Error | Meaning |
|---|---|
401, Bearer error="invalid_token" |
The token has no role, or the wrong audience. Grant the Application permission with admin consent, or make the managed identity's app role assignment. A managed identity token requested before the role existed is cached for up to 24 hours. |
| 401, invalid audience | The scope is not the Analytics API's Application ID URI. |
| 403 | The role is too low for the call, for example DataReader on a write. |
ManagedIdentityCredential authentication unavailable |
The code is not running on an Azure resource with a managed identity turned on. Use a service principal when running locally. |
Was this article helpful?
That’s Great!
Thank you for your feedback
Sorry! We couldn't be helpful
Thank you for your feedback
Feedback sent
We appreciate your effort and will try to fix the article