Microsoft 365 Agents SDK (.NET)
使用 .NET 构建 Microsoft 365 智能体
构建面向 Microsoft 365 的多通道智能体需要了解 Microsoft.Agents SDK 托管模型、身份验证模式和路由架构。本技能提供生产级代码示例,涵盖 ASP.NET Core 托管、MSAL 身份验证和 Copilot Studio 集成。
下载技能 ZIP
在 Claude 中上传
前往 设置 → 功能 → 技能 → 上传技能
开启并开始使用
测试它
正在使用“Microsoft 365 Agents SDK (.NET)”。 Create an AgentApplication subclass with a message handler that echoes user input
预期结果:
Generates a C# class inheriting AgentApplication with constructor registering OnActivity(ActivityTypes.Message, OnMessageAsync), and an OnMessageAsync method that calls turnContext.SendActivityAsync with the echoed text.
正在使用“Microsoft 365 Agents SDK (.NET)”。 Generate appsettings.json with TokenValidation and ServiceConnection configuration
预期结果:
Produces a JSON configuration with TokenValidation section containing Enabled, Audiences array, TenantId, and Connections section with AuthType, ClientId, ClientSecret, AuthorityEndpoint, and Scopes for api.botframework.com.
正在使用“Microsoft 365 Agents SDK (.NET)”。 Show how to add authorization to the /api/messages endpoint in production
预期结果:
Shows wrapping the POST route with if (!app.Environment.IsDevelopment()) { incomingRoute.RequireAuthorization(); } to enforce authentication outside development environment.
安全审计
安全Documentation-only skill with no executable code. Contains C# code examples and configuration templates for Microsoft 365 Agents SDK. No scripts, network operations, file system access, or external commands detected. Static analysis correctly identified this as a prompt-only skill with zero security risk.
质量评分
你能构建什么
创建带身份验证的 Teams 机器人
生成一个生产级 ASP.NET Core 机器人,通过 MSAL 验证用户身份,并在 Microsoft Teams 频道中响应消息。
与 Copilot Studio 集成
构建控制台或 Web 应用,使用直连引擎客户端模式和委托令牌连接到 Copilot Studio。
实现智能体错误处理
为 AgentApplication 子类添加全面的错误处理和状态管理,包括正确的清理和对话结束流程。
试试这些提示
Generate an ASP.NET Core Minimal API host for a Microsoft.Agents SDK bot. Include AddAgentApplicationOptions, AddAgent for MyAgent class, MemoryStorage, AddAgentAspNetAuthentication, and a POST /api/messages endpoint with IAgentHttpAdapter.
Create an AgentApplication subclass called SupportAgent with OnConversationUpdate for welcome messages, OnActivity for ActivityTypes.Message routed to a handler method, and OnTurnError for exception handling with state cleanup.
Create a DelegatingHandler called AddTokenHandler that acquires tokens using MSAL PublicClientApplication with AcquireTokenSilent fallback to AcquireTokenInteractive. Include Bearer header injection and CopilotClient.ScopeFromSettings usage.
Generate a HostApplicationBuilder console application that registers CopilotClient with AddHttpClient, custom DelegatingHandler for tokens, and uses StartConversationAsync and AskQuestionAsync to interact with Copilot Studio.
最佳实践
- 使用 AgentApplication 基类将路由逻辑、事件处理器和错误处理集中在一个位置,而不是分散在中间件中。
- 对于生产部署,将 MemoryStorage 替换为 BlobStorage 或 CosmosDbStorage 等持久化存储,以在服务器重启后保持对话状态。
- 在 appsettings.json 中启用 TokenValidation,并将 RequireAuthorization 应用于 /api/messages 端点,以防止未经授权访问您的智能体。
避免
- 将 ClientSecret 或其他凭据直接存储在代码中,而不是使用 Azure Key Vault、环境变量或托管标识等配置提供程序。
- 为每个请求创建新的 HttpClient 实例,而不是使用 IHttpClientFactory 来高效注册和重用 HTTP 连接。
- 通过使用 .Result 或 .Wait() 同步阻塞异步调用,而不是正确地 await 异步 SDK 方法并传递 CancellationToken。