Skip to content

Added retry counter for acquire token and modified added a test with …#3682

Merged
bgavrilMS merged 2 commits into
masterfrom
4gust/infinite-retry-fix
Feb 9, 2026
Merged

Added retry counter for acquire token and modified added a test with …#3682
bgavrilMS merged 2 commits into
masterfrom
4gust/infinite-retry-fix

Conversation

@4gust

@4gust 4gust commented Jan 27, 2026

Copy link
Copy Markdown
Contributor

Fix Certificate Reload Infinite Recursion Bug
Description
Certificate reload logic was triggering for all invalid_client errors, not just certificate-related ones. This caused unnecessary retries for unrelated authentication failures like wrong passwords or missing app registrations.
Additionally, the shared _retryClientCertificate boolean flag had thread-safety issues in concurrent scenarios.
Solution
Restored specific error checking - Only reload certificates for actual cert errors:
• AADSTS700027 - Invalid key
• AADSTS700024 - Invalid time range
• AADSTS7000214 - Certificate revoked
• AADSTS1000502 - Certificate expired Replaced shared flag with per-call counter - Each call tracks its own retry count (max 1 retry),
preventing infinite loops and other conditions.
Fixes issues :
#3654

Changes
• Added MaxCertificateRetries = 1 constant
• Updated IsInvalidClientCertificateOrSignedAssertionError(MsalServiceException) to accept retryCount parameter
• Retry logic now distinguishes between legitimate cert errors (can retry) vs config errors (no retry)
• Added retry counter tracking through token acquisition call stack

@4gust 4gust requested a review from a team as a code owner January 27, 2026 15:05

@neha-bhargava neha-bhargava 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.

This looks good. Another option would be to use the extensibility API https://github.com/AzureAD/microsoft-authentication-library-for-dotnet/blob/main/src/client/Microsoft.Identity.Client/Extensibility/ConfidentialClientApplicationBuilderExtensions.cs#L123. This way you would just have to configure the retry in the callback. Add it to the CCA builder. @bgavrilMS thoughts? I think that would be a cleaner approach.

@neha-bhargava neha-bhargava 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.

Approving if you would like to go with this approach.

@bgavrilMS

bgavrilMS commented Feb 9, 2026

Copy link
Copy Markdown
Member

This looks good. Another option would be to use the extensibility API https://github.com/AzureAD/microsoft-authentication-library-for-dotnet/blob/main/src/client/Microsoft.Identity.Client/Extensibility/ConfidentialClientApplicationBuilderExtensions.cs#L123. This way you would just have to configure the retry in the callback. Add it to the CCA builder. @bgavrilMS thoughts? I think that would be a cleaner approach.

But in this case, the entire TokenAcquirer needs to be reset, so as to force a certificate re-load:

image

I don't think this can be done with OnMsalFailure, because you can't get a CCA instance which was created with WithCertificate to reload the cert. We'd have to move to use WithCertificate(() => x509) which is a lot of work.

@bgavrilMS bgavrilMS merged commit 0f0f93e into master Feb 9, 2026
4 checks passed
@bgavrilMS bgavrilMS deleted the 4gust/infinite-retry-fix branch February 9, 2026 18:09
This was referenced Mar 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants