Repository navigation
Include scheme in certificate cache for Authentication middleware - #66673
Merged
DeagleGross merged 1 commit intoMay 14, 2026
Merged
Conversation
…scheme in certificate cache Storing scheme as a string in `HttpContext.Items` under `__CertificateAuthScheme`, which is used as part of cache key now. (cherry picked from commit b0b333d)
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates certificate authentication caching so cached validation results are isolated per authentication scheme by including the scheme name in the cache key (propagated via HttpContext.Items), and adds tests covering the new behavior.
Changes:
- Include the scheme name in the certificate validation cache key and no-op caching when the scheme marker is missing.
- Set a per-request scheme marker in
CertificateAuthenticationHandlerto support scheme-scoped cache keys. - Add tests validating cross-scheme cache isolation and the cache no-op behavior without the scheme marker.
Show a summary per file
| File | Description |
|---|---|
| src/Security/Authentication/test/CertificateTests.cs | Adds tests for scheme-isolated cache behavior and missing-scheme no-op behavior. |
| src/Security/Authentication/Certificate/src/CertificateValidationCache.cs | Changes cache key computation to include scheme (from HttpContext.Items) and handles missing scheme. |
| src/Security/Authentication/Certificate/src/CertificateAuthenticationHandler.cs | Stores the current scheme into HttpContext.Items before performing cache lookup. |
Copilot's findings
- Files reviewed: 3/3 changed files
- Comments generated: 3
Comment on lines
70
to
74
| if (_cache != null) | ||
| { | ||
| Context.Items[CertificateSchemeCacheKeyItem] = Scheme.Name; | ||
| var cacheHit = _cache.Get(Context, clientCertificate); | ||
| if (cacheHit != null) |
Comment on lines
41
to
+49
| public AuthenticateResult? Get(HttpContext context, X509Certificate2 certificate) | ||
| => _cache.Get<AuthenticateResult>(ComputeKey(certificate))?.Clone(); | ||
| { | ||
| var key = ComputeKey(context, certificate); | ||
| if (key is null) | ||
| { | ||
| return null; | ||
| } | ||
| return _cache.Get<AuthenticateResult>(key)?.Clone(); | ||
| } |
Comment on lines
+78
to
+83
| private static string? ComputeKey(HttpContext context, X509Certificate2 certificate) | ||
| { | ||
| if (context.Items.TryGetValue(CertificateAuthenticationHandler.CertificateSchemeCacheKeyItem, out var schemeObj) | ||
| && schemeObj is string schemeName) | ||
| { | ||
| return $"{schemeName}:{certificate.GetCertHashString(HashAlgorithmName.SHA256)}"; |
javiercn
approved these changes
May 13, 2026
BrennanConroy
approved these changes
May 13, 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.
Storing scheme as a string in HttpContext.Items under __CertificateAuthScheme, which is used as part of cache key now.