Edit

Call APIs as an agent's user account in a fleet with ASP.NET Core

Some agents in a fleet need a user object for Microsoft 365 workloads, such as a mailbox or Teams membership. For those agents, look up both the agent identity and its agent's user account in the agent directory, and pass them to WithAgentUserIdentity.

To decide whether an agent needs an agent's user account, see Use an agent's user account with Microsoft 365.

Prerequisites

Get a token for an agent's user account

Add a method to FleetTokenService:

using System.Security.Claims;

public async Task<string> CreateAuthorizationHeaderForAgentUserAsync(
    string agentId, string scope)
{
    AgentRegistration registration = await GetRegistrationAsync(agentId);

    if (registration.AgentUserId is null)
    {
        throw new InvalidOperationException(
            $"Agent '{agentId}' doesn't have an agent's user account.");
    }

    var options = new AuthorizationHeaderProviderOptions()
        .WithAgentUserIdentity(
            registration.AgentIdentityId,
            Guid.Parse(registration.AgentUserId));

    return await authorizationHeaderProvider
        .CreateAuthorizationHeaderForUserAsync([scope], options, new ClaimsPrincipal());
}

Pass new ClaimsPrincipal() because no person is signed in. The user comes from the agent directory, not from the request context.

Call the method from the agent's work, and send the header to Microsoft Graph:

using System.Net.Http.Headers;

string authorizationHeader = await fleetTokenService
    .CreateAuthorizationHeaderForAgentUserAsync(agentId, "https://graph.microsoft.com/.default");

HttpClient client = httpClientFactory.CreateClient();
client.DefaultRequestHeaders.Authorization =
    AuthenticationHeaderValue.Parse(authorizationHeader);

using HttpResponseMessage response =
    await client.GetAsync("https://graph.microsoft.com/v1.0/me/messages");
response.EnsureSuccessStatusCode();

The token represents the agent's user account, so /me returns that account's data. Each agent can reach only the mailbox, files, and teams that its own account was given.

For this example, verify that Microsoft Graph returns a successful messages response for the selected agent's mailbox. An empty messages collection isn't an authentication failure. If the call fails, check delegated permission grants separately from the account's license, mailbox provisioning, and resource access; acquiring a token doesn't provision a mailbox.

Next step

Authenticate users and route requests to agents in a fleet with ASP.NET Core