この記事では、エージェント ID またはエージェントのユーザー アカウントを使用して、エージェントから Microsoft Graph APIを呼び出す方法について説明します。
エージェントから API を呼び出すには、エージェントが API に対して自身を認証するために使用できるアクセス トークンを取得する必要があります。 Microsoft.Identity.Web SDK for .NET を使用して Web API を呼び出すことをお勧めします。 この SDK は、トークンの取得と検証のプロセスを簡略化します。 他の言語の場合は、Microsoft Entra エージェント SDK をエージェント ID のために使用します。
前提条件
- ターゲット API を呼び出す適切なアクセス許可を持つエージェント ID。 代理操作のフローにはユーザーが必要です。
- ターゲット API を呼び出す適切なアクセス許可を持つエージェントのユーザー アカウント。
Microsoft Graph APIを呼び出す
Microsoft.Identity.Web.GraphServiceClient をインストールして、Graph SDK の認証を処理し、Microsoft.Identity.Web.AgentIdentities パッケージをインストールしてエージェント ID のサポートを追加します。
dotnet add package Microsoft.Identity.Web.GraphServiceClient dotnet add package Microsoft.Identity.Web.AgentIdentitiesサービス コレクションにMicrosoft Graph ID とエージェント ID のサポートを追加します。
using Microsoft.Identity.Web; var builder = WebApplication.CreateBuilder(args); // Add authentication (web app or web API) builder.Services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme) .AddMicrosoftIdentityWebApp(builder.Configuration.GetSection("AzureAd")) .EnableTokenAcquisitionToCallDownstreamApi() .AddInMemoryTokenCaches(); // Add Microsoft Graph support builder.Services.AddMicrosoftGraph(); // Add Agent Identities support builder.Services.AddAgentIdentities(); var app = builder.Build(); app.UseAuthentication(); app.UseAuthorization(); app.Run();appsettings.jsonで Graph とエージェント ID のオプションを構成します。
Warnung
セキュリティ リスクのために、エージェント ID ブループリントの運用環境では、クライアント シークレットをクライアント資格情報として使用しないでください。 代わりに、マネージド ID またはクライアント証明書を使用 するフェデレーション ID 資格情報 (FIC) などのより安全な認証方法を使用してください。 これらの方法により、アプリケーション構成内に機密性の高いシークレットを直接格納する必要がなくなり、セキュリティが強化されます。
{ "AzureAd": { "Instance": "https://login.microsoftonline.com/", "TenantId": "<my-test-tenant>", "ClientId": "<agent-blueprint-client-id>", "ClientCredentials": [ { "SourceType": "ClientSecret", "ClientSecret": "your-client-secret" } ] }, "DownstreamApis": { "MicrosoftGraph": { "BaseUrl": "https://graph.microsoft.com/v1.0", "Scopes": ["User.Read", "User.ReadBasic.All"] } } }現在、
GraphServiceClientをサービスやサービス プロバイダーから取得し、それを注入して Microsoft Graph を呼び出すことができます。
エージェント ID の場合は、
WithAgentIdentityメソッドを使用して、アプリ専用トークン (自律エージェント) またはユーザー トークンの代理 (対話型エージェント) を取得できます。 アプリのみのトークンの場合は、RequestAppTokenプロパティをtrueに設定します。 ユーザー トークンの代理委任の場合は、RequestAppTokenプロパティを設定したり、明示的にfalseに設定したりしないでください。// Get the GraphServiceClient GraphServiceClient graphServiceClient = serviceProvider.GetRequiredService<GraphServiceClient>(); string agentIdentity = "agent-identity-guid"; // Call Microsoft Graph APIs with the agent identity for app only scenario var applications = await graphServiceClient.Applications .GetAsync(r => r.Options.WithAuthenticationOptions(options => { options.WithAgentIdentity(agentIdentity); options.RequestAppToken = true; // Set to true for app only })); // Call Microsoft Graph APIs with the agent identity for on-behalf of user scenario var applications = await graphServiceClient.Applications .GetAsync(r => r.Options.WithAuthenticationOptions(options => { options.WithAgentIdentity(agentIdentity); options.RequestAppToken = false; // False to show it's on-behalf of user }));エージェントのユーザー アカウント ID の場合は、ユーザー プリンシパル名 (UPN) またはオブジェクト ID (OID) のいずれかを指定して、
WithAgentUserIdentityメソッドを使用してエージェントのユーザー アカウントを識別できます。// Get the GraphServiceClient GraphServiceClient graphServiceClient = serviceProvider.GetRequiredService<GraphServiceClient>(); string agentIdentity = "agent-identity-guid"; // Call Microsoft Graph APIs with the agent's user account identity using UPN string userUpn = "user-upn"; var me = await graphServiceClient.Me .GetAsync(r => r.Options.WithAuthenticationOptions(options => options.WithAgentUserIdentity(agentIdentity, userUpn))); // Or using OID string userOid = "user-object-id"; var me = await graphServiceClient.Me .GetAsync(r => r.Options.WithAuthenticationOptions(options => options.WithAgentUserIdentity(agentIdentity, userOid)));