.NET: AuthN & AuthZ sample with asp.net service and web client#4354
Merged
westey-m merged 8 commits intomicrosoft:mainfrom Mar 2, 2026
Merged
.NET: AuthN & AuthZ sample with asp.net service and web client#4354westey-m merged 8 commits intomicrosoft:mainfrom
westey-m merged 8 commits intomicrosoft:mainfrom
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR adds a comprehensive authentication and authorization sample demonstrating how to secure an AI agent REST API using OAuth 2.0 / OpenID Connect standards. The sample addresses issue #1488 by showcasing dependency injection, user context access in tools, JWT Bearer authentication, and policy-based authorization.
Changes:
- Added a three-component sample (WebClient, AgentService, Keycloak) orchestrated with Docker Compose
- Demonstrates accessing user identity in agent tools via scoped IUserContext service
- Includes GitHub Codespaces support with auto-detection and configuration
- Shows JWT Bearer token validation and scope-based authorization policies
Reviewed changes
Copilot reviewed 24 out of 24 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| docker-compose.yml | Orchestrates Keycloak, AgentService, and WebClient with health checks and Codespaces support |
| keycloak/dev-realm.json | Pre-configured Keycloak realm with test users, scopes, and client definitions |
| keycloak/setup-redirect-uris.sh | Auto-configures Codespaces redirect URIs for OIDC flows |
| README.md | Comprehensive documentation for setup, usage, and key concepts |
| AuthClientServer.AgentService/* | Minimal API with JWT authentication, authorization policies, and DI-based agent |
| AuthClientServer.WebClient/* | Razor Pages app with OIDC login, token forwarding, and chat UI |
| UserContext.cs | Scoped service for accessing current user identity from JWT claims |
| TodoService.cs | Per-user TODO list demonstrating user-aware tool implementation |
| Dockerfiles | Multi-stage builds with proper CPM support via Directory.Packages.props |
| Directory.Packages.props | Added JWT Bearer and OpenIdConnect package versions |
| agent-framework-dotnet.slnx | Solution file updated to include new projects |
dotnet/samples/05-end-to-end/AspNetAgentAuthorization/keycloak/setup-redirect-uris.sh
Show resolved
Hide resolved
dotnet/samples/05-end-to-end/AuthClientServer/AuthClientServer.AgentService/Program.cs
Outdated
Show resolved
Hide resolved
dotnet/samples/05-end-to-end/AuthClientServer/AuthClientServer.AgentService/Program.cs
Outdated
Show resolved
Hide resolved
dotnet/samples/05-end-to-end/AspNetAgentAuthorization/Service/Service.csproj
Show resolved
Hide resolved
dotnet/samples/05-end-to-end/AspNetAgentAuthorization/RazorWebClient/Pages/Chat.cshtml
Show resolved
Hide resolved
SergeyMenshykh
approved these changes
Mar 2, 2026
rogerbarreto
approved these changes
Mar 2, 2026
This was referenced Mar 4, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation and Context
#1488
Users are regularly asking about how to access user information in tools and how to authorize tools. This sample shows an example of how authorization and user access could be done from tools in an agent.
Using Keycloak as the auth provider, so that the sample can be run without any external dependencies, but it can easily be swapped out for another auth provider.
Description
Contribution Checklist