Skip to content

Include scheme in certificate cache for Authentication middleware - #66673

Merged
DeagleGross merged 1 commit into
dotnet:mainfrom
DeagleGross:dmkorolev/rel9/addcertificate-scheme
May 14, 2026
Merged

DeagleGross merged 1 commit into
dotnet:mainfrom
DeagleGross:dmkorolev/rel9/addcertificate-scheme

Conversation

@DeagleGross

Copy link
Copy Markdown
Member

Storing scheme as a string in HttpContext.Items under __CertificateAuthScheme, which is used as part of cache key now.

…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)
@DeagleGross
DeagleGross requested a review from halter73 as a code owner May 13, 2026 12:34
Copilot AI review requested due to automatic review settings May 13, 2026 12:34
@github-actions github-actions Bot added the area-auth Includes: authentication, authorization, OAuth, OIDC, and access token validation label May 13, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 CertificateAuthenticationHandler to 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)}";
@DeagleGross
DeagleGross enabled auto-merge (squash) May 14, 2026 11:26
@DeagleGross
DeagleGross merged commit fde6094 into dotnet:main May 14, 2026
31 checks passed
@DeagleGross
DeagleGross deleted the dmkorolev/rel9/addcertificate-scheme branch May 14, 2026 11:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-auth Includes: authentication, authorization, OAuth, OIDC, and access token validation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants