この記事では、エージェントから Azure サービスを呼び出す方法について説明します。 エージェント ID を使用してAzure StorageやAzure Key VaultなどのAzure サービスに対して認証を行うには、Microsoft の MicrosoftIdentityTokenCredential クラスを使用します。Identity.Web。Azure。
MicrosoftIdentityTokenCredential クラスは、Azure SDK の TokenCredential インターフェイスを実装することで、Microsoft.Identity.Web と Azure SDK クライアント間のシームレスな統合を可能にします。
エージェントから API を呼び出すには、エージェントが API に対して自身を認証するために使用できるアクセス トークンを取得する必要があります。 .NET用 SDKであるMicrosoft.Identity.Webを使用してWeb API を呼び出すことをお勧めします。 この SDK は、トークンの取得と検証のプロセスを簡略化します。 他の言語の場合は、Microsoft Entra エージェント SDKをエージェント IDのために使用します。
前提条件
- ターゲット API を呼び出す適切なアクセス許可を持つエージェント ID。 代理操作のフローにはユーザーが必要です。
- ターゲット API を呼び出す適切なアクセス許可を持つエージェントのユーザー アカウント。
実装の手順
Azure統合パッケージとMicrosoft.Identity.Web.AgentIdentitiesパッケージをインストールして、エージェント ID のサポートを追加します。
dotnet add package Microsoft.Identity.Web.Azure dotnet add package Microsoft.Identity.Web.AgentIdentitiesAzure Storageなど、使用するAzure SDK パッケージをインストールします。
dotnet add package Azure.Storage.BlobsAzureトークン資格情報のサポートを追加するようにサービスを構成します。
using Microsoft.Identity.Web; var builder = WebApplication.CreateBuilder(args); // Add authentication builder.Services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme) .AddMicrosoftIdentityWebApp(builder.Configuration.GetSection("AzureAd")) .EnableTokenAcquisitionToCallDownstreamApi() .AddInMemoryTokenCaches(); // Add Azure token credential support builder.Services.AddMicrosoftIdentityAzureTokenCredential(); builder.Services.AddControllersWithViews(); var app = builder.Build(); app.UseAuthentication(); app.UseAuthorization(); app.MapControllers(); app.Run();appsettings.jsonでAzureのトークン資格情報オプションを構成します。
Warnung
セキュリティ リスクのために、エージェント ID ブループリントの運用環境では、クライアント シークレットをクライアント資格情報として使用しないでください。 代わりに、マネージド ID またはクライアント証明書を使用 するフェデレーション ID 資格情報 (FIC) などのより安全な認証方法を使用してください。 これらの方法により、アプリケーション構成内に機密性の高いシークレットを直接格納する必要がなくなり、セキュリティが強化されます。
{ "AzureAd": { "Instance": "https://login.microsoftonline.com/", "TenantId": "<your-tenant>", "ClientId": "<agent-blueprint-id>", // Other client creedentials available. See <https://aka.ms/ms-id-web/client-credentials> "ClientCredentials": [ { "SourceType": "ClientSecret", "ClientSecret": "your-client-secret" } ] } }サービス プロバイダーからトークン資格情報を取得し、Azure SDK クライアントで使用します。
エージェント ID の場合は、
WithAgentIdentityメソッドを使用して、アプリ専用トークン (自律エージェント) またはユーザー トークンの代理 (対話型エージェント) を取得できます。 アプリのみのトークンの場合は、RequestAppTokenプロパティをtrueに設定します。 ユーザー トークンの代理委任の場合は、RequestAppTokenプロパティを設定したり、明示的にfalseに設定したりしないでください。using Microsoft.Identity.Web; public class AgentService { private readonly MicrosoftIdentityTokenCredential _credential; public AgentService(MicrosoftIdentityTokenCredential credential) { _credential = credential; } // Call Azure service with the agent identity for app only scenario public async Task<List<string>> ListBlobsForAgentAsync(string agentIdentity) { // Configure for agent identity string agentIdentity = "agent-identity-guid"; _credential.Options.WithAgentIdentity(agentIdentity); _credential.Options.RequestAppToken = true; var blobClient = new BlobServiceClient( new Uri("https://myaccount.blob.core.windows.net"), _credential); var container = blobClient.GetBlobContainerClient("agent-data"); var blobs = new List<string>(); await foreach (var blob in container.GetBlobsAsync()) { blobs.Add(blob.Name); } return blobs; } // Call Azure service with the agent identity for on-behalf of user scenario public async Task<List<string>> ListBlobsForAgentAsync(string agentIdentity) { // Configure for agent identity var blobClient = new BlobServiceClient( new Uri("https://myaccount.blob.core.windows.net")); var container = blobClient.GetBlobContainerClient("agent-data"); var blobs = new List<string>(); await foreach (var blob in container.GetBlobsAsync()) { blobs.Add(blob.Name); } return blobs; } }エージェントのユーザー アカウントのトークンを取得することもできます。 これを行うには、ユーザー プリンシパル名 (UPN) またはオブジェクト ID (OID) を使用して、エージェントのユーザー アカウントを識別できます。
オブジェクト ID の場合:
using Microsoft.Identity.Web; public class AgentService { private readonly MicrosoftIdentityTokenCredential _credential; public AgentService(MicrosoftIdentityTokenCredential credential) { _credential = credential; } // Use object ID to identify the agent's user account public async Task<List<string>> ListBlobsForAgentAsync(string agentIdentity) { // Configure for agent identity string agentIdentity = "agent-identity-guid"; string userOid = "user-object-id"; _credential.Options.WithAgentUserIdentity(agentIdentity, userOid); var blobClient = new BlobServiceClient( new Uri("https://myaccount.blob.core.windows.net"), _credential); var container = blobClient.GetBlobContainerClient("agent-data"); var blobs = new List<string>(); await foreach (var blob in container.GetBlobsAsync()) { blobs.Add(blob.Name); } return blobs; } // Use UPN to identify the agent's user account\ public async Task<List<string>> ListBlobsForAgentAsync(string agentIdentity) { // Configure for agent identity string agentIdentity = "agent-identity-guid"; string userUpn = "user@contoso.com"; _credential.Options.WithAgentUserIdentity(agentIdentity, userUpn); var blobClient = new BlobServiceClient( new Uri("https://myaccount.blob.core.windows.net"), _credential); var container = blobClient.GetBlobContainerClient("agent-data"); var blobs = new List<string>(); await foreach (var blob in container.GetBlobsAsync()) { blobs.Add(blob.Name); } return blobs; } }