-
Notifications
You must be signed in to change notification settings - Fork 1.9k
.NET: Support ClaimsIdentity-based scoping of agent sessions #5696
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
3f9ae83
feat: Add DelegatingAgentSessionStore
lokitoth f346f2a
feat: Add UserIdentityScopedSessionStore
lokitoth cebd351
fix: Harden scope mapping
lokitoth 4e18808
fix: Add UserIdentityScopeSessionStoreOptions to avoid future breakin…
lokitoth aa67761
Split UserIdentityScopedSessionStore into a separate IsolationKeyProv…
lokitoth 240f36b
Add GetService<>() capabilities to interrogate AgentSessionStore dele…
lokitoth a61e0f2
Harden default for A2A hosting by using an IsolationKeyScopedAgentSes…
lokitoth 635b4cb
Pipe isolation through Hosting helper extension methods
lokitoth 556d213
Add comment to samples about adding SessionIsolationKeyProvider
lokitoth 76a4bc1
Fix isolation key provider nullability semantics
lokitoth acd6b81
fix A2A defaults
lokitoth 0e6e729
fixup
lokitoth 90b0865
remove unneeded keyProvider requirement test
lokitoth da0d068
Add trust-model XML docs to AgentSessionStore, InMemoryAgentSessionSt…
Copilot 18efa9a
fix: Switch ClaimsBasedIsolationKeyProvider to be Singleton
lokitoth 8381e9c
release: Ensure new project is in the release filter
lokitoth 7a14757
fixup: Integraitaon tests
lokitoth File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
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
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
78 changes: 78 additions & 0 deletions
78
...t/src/Microsoft.Agents.AI.Hosting.AspNetCore/ClaimsIdentitySessionIsolationKeyProvider.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| using System; | ||
| using System.Linq; | ||
| using System.Security.Claims; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.AspNetCore.Http; | ||
| using Microsoft.Shared.Diagnostics; | ||
|
|
||
| namespace Microsoft.Agents.AI.Hosting; | ||
|
|
||
| /// <summary> | ||
| /// A <see cref="SessionIsolationKeyProvider"/> that extracts the session isolation key from a claim | ||
| /// in the current user's identity, as provided by ASP.NET Core's <see cref="IHttpContextAccessor"/>. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// <para> | ||
| /// This provider is suitable for ASP.NET Core web applications where session isolation is based on | ||
| /// authenticated user identity. It reads a specified claim type (e.g., name, email, or a custom identifier) | ||
| /// from the ambient <see cref="HttpContext"/>. | ||
| /// </para> | ||
| /// <para> | ||
| /// If the <see cref="HttpContext"/> is unavailable, the user is not authenticated, or the specified claim | ||
|
lokitoth marked this conversation as resolved.
|
||
| /// is missing, the provider returns <see langword="null"/>. The consuming <see cref="IsolationKeyScopedAgentSessionStore"/> | ||
| /// will then enforce strict or pass-through behavior based on its configuration. | ||
| /// </para> | ||
| /// <para> | ||
| /// This class relies on <see cref="IHttpContextAccessor"/>, which uses <see cref="AsyncLocal{T}"/> | ||
| /// to provide access to the current <see cref="HttpContext"/>. | ||
| /// </para> | ||
| /// </remarks> | ||
| public class ClaimsIdentitySessionIsolationKeyProvider : SessionIsolationKeyProvider | ||
| { | ||
| private readonly IHttpContextAccessor? _httpContextAccessor; | ||
| private readonly string _claimType; | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="ClaimsIdentitySessionIsolationKeyProvider"/> class. | ||
| /// </summary> | ||
| /// <param name="httpContextAccessor"> | ||
| /// The <see cref="IHttpContextAccessor"/> used to retrieve the current HTTP context and user claims. | ||
| /// </param> | ||
| /// <param name="options">The options for configuring the provider. If null, defaults are used.</param> | ||
| /// <exception cref="ArgumentException"> | ||
| /// <see cref="ClaimsIdentitySessionIsolationKeyProviderOptions.ClaimType"/> is null, empty, or whitespace. | ||
| /// </exception> | ||
| public ClaimsIdentitySessionIsolationKeyProvider( | ||
| IHttpContextAccessor? httpContextAccessor, | ||
| ClaimsIdentitySessionIsolationKeyProviderOptions? options = null) | ||
| { | ||
| options ??= new ClaimsIdentitySessionIsolationKeyProviderOptions(); | ||
| this._httpContextAccessor = httpContextAccessor; | ||
| this._claimType = Throw.IfNullOrWhitespace(options.ClaimType); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Extracts the session isolation key from the current user's claims. | ||
| /// </summary> | ||
| /// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests.</param> | ||
| /// <returns> | ||
| /// A task that represents the asynchronous operation. The task result contains the value of the | ||
| /// configured claim type from the current user's identity, or <see langword="null"/> if the claim | ||
| /// is not present or the HTTP context is unavailable. | ||
| /// </returns> | ||
| /// <remarks> | ||
| /// This method retrieves the claim value from <c>HttpContext.User.Claims</c>. If multiple claims | ||
| /// of the specified type exist, the first match is returned. | ||
| /// </remarks> | ||
| public override ValueTask<string?> GetSessionIsolationKeyAsync(CancellationToken cancellationToken = default) | ||
| { | ||
| Claim? claim = this._httpContextAccessor? | ||
| .HttpContext? | ||
| .User?.Claims.FirstOrDefault(c => c.Type == this._claimType); | ||
|
|
||
| return new ValueTask<string?>(claim?.Value); | ||
| } | ||
| } | ||
30 changes: 30 additions & 0 deletions
30
...icrosoft.Agents.AI.Hosting.AspNetCore/ClaimsIdentitySessionIsolationKeyProviderOptions.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| using System.Security.Claims; | ||
|
|
||
| namespace Microsoft.Agents.AI.Hosting; | ||
|
|
||
| /// <summary> | ||
| /// Options for configuring <see cref="ClaimsIdentitySessionIsolationKeyProvider"/>. | ||
| /// </summary> | ||
| public class ClaimsIdentitySessionIsolationKeyProviderOptions | ||
| { | ||
| /// <summary> | ||
| /// Gets or sets the claim type to extract from the user's identity for session isolation. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// <para> | ||
| /// Defaults to <see cref="ClaimsIdentity.DefaultNameClaimType"/>, which typically corresponds to | ||
| /// the user's name or unique identifier claim. | ||
| /// </para> | ||
| /// <para> | ||
| /// Common alternatives include: | ||
| /// <list type="bullet"> | ||
| /// <item><description><c>ClaimTypes.NameIdentifier</c> — Stable user identifier</description></item> | ||
| /// <item><description><c>ClaimTypes.Email</c> — Email address</description></item> | ||
| /// <item><description>Custom claim types specific to your authentication provider</description></item> | ||
| /// </list> | ||
| /// </para> | ||
| /// </remarks> | ||
| public string ClaimType { get; set; } = ClaimsIdentity.DefaultNameClaimType; | ||
| } |
30 changes: 30 additions & 0 deletions
30
.../src/Microsoft.Agents.AI.Hosting.AspNetCore/Microsoft.Agents.AI.Hosting.AspNetCore.csproj
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFrameworks>$(TargetFrameworksCore)</TargetFrameworks> | ||
| <RootNamespace>Microsoft.Agents.AI.Hosting.AspNetCore</RootNamespace> | ||
| <VersionSuffix>preview</VersionSuffix> | ||
| <NoWarn>$(NoWarn)</NoWarn> | ||
| </PropertyGroup> | ||
|
|
||
| <Import Project="$(RepoRoot)/dotnet/nuget/nuget-package.props" /> | ||
|
|
||
| <PropertyGroup> | ||
| <InjectSharedThrow>true</InjectSharedThrow> | ||
| <InjectSharedDiagnosticIds>true</InjectSharedDiagnosticIds> | ||
| <InjectExperimentalAttributeOnLegacy>true</InjectExperimentalAttributeOnLegacy> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <FrameworkReference Include="Microsoft.AspNetCore.App" /> | ||
| </ItemGroup> | ||
| <ItemGroup> | ||
| <ProjectReference Include="..\Microsoft.Agents.AI.Hosting\Microsoft.Agents.AI.Hosting.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| <PropertyGroup> | ||
| <!-- NuGet Package Settings --> | ||
| <Title>Microsoft Agent Framework Hosting ASP.NET Core</Title> | ||
| <Description>Provides Microsoft Agent Framework support for hosting agents in an ASP.NET Core context.</Description> | ||
| </PropertyGroup> | ||
| </Project> |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.