Edit

Call Microsoft Graph as an agent with ASP.NET Core

List directory users with the agent's application permissions, not the inbound caller's delegated access.

In this article, you configure GraphServiceClient to obtain a Microsoft Graph token for the agent while the agent API separately authorizes the caller.

Prerequisites

Add Microsoft Graph

Install the integration package:

dotnet add package Microsoft.Identity.Web.GraphServiceClient

Register GraphServiceClient with the services configured in the parent article. For app-only access, call builder.Services.AddMicrosoftGraph with options.BaseUrl set to https://graph.microsoft.com/v1.0 and options.Scopes set to ["https://graph.microsoft.com/.default"]. Use the configured client ID directly; don't add WithAgentIdentity.

Place this registration before builder.Build() in the agent API's Program.cs.

builder.Services.AddMicrosoftGraph(options =>
{
    options.BaseUrl = "https://graph.microsoft.com/v1.0";
    options.Scopes = ["https://graph.microsoft.com/.default"];
});

Call Microsoft Graph

Set RequestAppToken to true so GraphServiceClient acquires an app-only token for the agent identity:

Merge the following using directives with those at the top of Program.cs, and insert the endpoint before its single app.Run(). You can omit the custom API endpoint.

using Microsoft.Graph;
using Microsoft.Identity.Web;

app.MapGet("/graph-users", async (GraphServiceClient graphClient) =>
{
    var users = await graphClient.Users.GetAsync(request =>
        request.Options.WithAuthenticationOptions(options =>
        {
            options.RequestAppToken = true;
        }));

    return Results.Ok(users?.Value);
})
.RequireAuthorization("AgentUser");

The client uses the shared client ID and blueprint credential configured in the parent article. Don't add an Agent ID override. The example requires an application permission that can list users, such as User.Read.All. Grant only the permissions required by your calls.

Verify the Graph call

Follow the parent journey's API access test, replacing /custom-api with /graph-users. Expect 200 and a JSON collection of users when the agent has the required Graph application permission. This endpoint returns the first response page, not necessarily every user in the directory.

Without an inbound token, expect 401. With an otherwise valid token missing Agent.User, expect 403 if the tenant issues that token. A downstream Graph failure causes the sample to throw rather than returning that same status from the agent. Verify Graph consent on the agent identity separately from the caller's role assignment.

Next step

Authenticate users to the agent API