Voorbeeldcode — Graph API
Kennisbank / Voorbeeldcode — Graph API
Demo modus API

Graph API Voorbeeldcode

Herbruikbare C# snippets voor de automatisering van de B2C → Entra External ID migratie via Microsoft Graph API.

Graph API authenticatie via Client Credentials
C# / .NET 8

Gebruik Microsoft.Graph en Azure.Identity NuGet-packages. Configureer een App Registration in Entra met de rechten Application.ReadWrite.All en Directory.ReadWrite.All.

// NuGet: dotnet add package Microsoft.Graph
// NuGet: dotnet add package Azure.Identity

using Azure.Identity;
using Microsoft.Graph;

// App Registration gegevens (sla op in appsettings of Key Vault)
var tenantId     = "YOUR_TENANT_ID";       // bijv. crowssodevelopment.onmicrosoft.com
var clientId     = "YOUR_CLIENT_ID";
var clientSecret = "YOUR_CLIENT_SECRET";

var credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
var graphClient = new GraphServiceClient(credential);

// Test: haal de tenant-info op
var org = await graphClient.Organization.GetAsync();
Console.WriteLine($"Verbonden met: {org?.Value?.FirstOrDefault()?.DisplayName}");
appsettings.json configuratie
JSON
{
  "GraphApi": {
    "TenantId":     "crowssodevelopment.onmicrosoft.com",
    "ClientId":     "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
    "ClientSecret": "~uw~secret~hier~"
  },
  "B2C": {
    "TenantId":     "uwtenant.onmicrosoft.com",
    "ClientId":     "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
    "ClientSecret": "~b2c~secret~hier~"
  }
}
Alle snippets zijn gebaseerd op Microsoft Graph SDK v5 (.NET 8). Benodigde app-rechten: Application.ReadWrite.All, Directory.ReadWrite.All, User.ReadWrite.All. Verleen consent via: Azure Portal → App Registration → API permissions → Grant admin consent.