Certificate authentication retry clears in-memory credentials and hides the original error

Open
#4,152 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 2 days

@bgavrilMS is already working on this.

Since Nov 26, 2026.

  • #4153 by @bgavrilMS — open

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
52/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
csharp

Research direction

Read CredentialsProvider.NotifyCertificateAction and DefaultCredentialsLoader.ResetCredentials, then trace retry credential loading and how IDW10109 preserves exceptions. Add network-free regression coverage for certificate recovery, exception chains, shared descriptions, custom loaders and warning-level logging. Done means failed authentication leaves caller-owned certificates usable, the original service exception is retained, and the reported regression cases pass.

Written by the indexing model from the issue text.

Description

Issue details

Microsoft.Identity.Web Library

Microsoft.Identity.Web.TokenAcquisition and Microsoft.Identity.Web.Certificate

Microsoft.Identity.Web version

Reproduced from master at 432c6daa on .NET 8 using network-free tests. A released-package reproduction has not been run.

Web app

Not Applicable

Web API

Not Applicable (app-only token acquisition)

Token cache serialization

In-memory caches; the credential mutation is independent of the cache provider.

Description

A certificate-related token-endpoint rejection can permanently disable a caller-supplied in-memory certificate credential.

The bounded retry reports unsuccessful certificate usage. CredentialsProvider.NotifyCertificateAction clears both CredentialDescription.Certificate and CachedValue. For CredentialSource.Certificate, the default credentials loader cannot reconstruct the certificate. Later acquisitions then fail locally with IDW10109, even if the token endpoint would accept the certificate.

The original MsalServiceException is also lost when retry preparation fails: IDW10109 only retains exceptions raised by credential loaders. Clearing the certificate changes its credential ID, which is included in the confidential-client application cache key, and affects consumers sharing the description.

Reproduction steps
  1. Supply an in-memory certificate using the configuration below.
  2. Use a fake IMsalHttpClientFactory whose token endpoint returns HTTP 401 with {"error":"invalid_client","error_description":"AADSTS700027: Invalid certificate","error_codes":[700027]} for the first request and accepts subsequent requests.
  3. Acquire an app token repeatedly using the same credential description.
Error message

IDW10109: No credential could be loaded.

The retry can produce empty loading details and no inner exception. Later calls fail without another token-endpoint request.

Id Web logs

The certificate-retry message and IDW10501 credential/build failure currently use an Information-level logging helper, so Warning-only logging can miss the triggering failure.

Relevant code snippets
options.ClientCredentials =
[
    new CredentialDescription
    {
        SourceType = CredentialSource.Certificate,
        Certificate = certificate,
    },
];
Regression

The first affected release has not been established.

Expected behavior
  • Failed authentication must not erase a caller-owned certificate or permanently prevent later acquisitions.
  • Keep bounded retries and existing custom loader/observer extensibility.
  • If retry credential loading fails, preserve the original token-endpoint exception along with any credential-loading exception.
  • Surface certificate-recovery and credential/build failures at Warning level without changing unrelated authentication logging.
Proposed fix

Align deselection with DefaultCredentialsLoader.ResetCredentials: always clear CachedValue, but preserve Certificate for CredentialSource.Certificate. Preserve the original service exception when credential loading fails during a retry. Add network-free recovery, exception-chain, cache/shared-description, custom-loader and logging regression tests.

The initial regression run against unfixed master produced 16 failures and 3 passing controls across 19 cases. After the fix, the final 21-case regression set passed on .NET 8, 9 and 10.

Dominant language
C#
Stars
787
Forks
277
Avg merge
1d 21h
Merged PRs (30d)
12

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from AzureAD/microsoft-identity-web

All issues in AzureAD/microsoft-identity-web

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.