Fix WindowsCertificateManager to correctly detect user cancellation of trust dialog#66604
Open
Fix WindowsCertificateManager to correctly detect user cancellation of trust dialog#66604
Conversation
1 task
…ation Agent-Logs-Url: https://github.com/dotnet/aspnetcore/sessions/d251b35d-591f-48d3-9a0f-38022f445c36 Co-authored-by: Youssef1313 <31348972+Youssef1313@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix cancellation reporting in WindowsCertificateManager
Fix WindowsCertificateManager to correctly detect user cancellation of trust dialog
May 6, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates Windows certificate trust handling to correctly recognize when a user cancels the Windows trust prompt, so the cancellation can be translated into UserCancelledTrustException instead of surfacing as an unhandled CryptographicException.
Changes:
- Updates the
CryptographicExceptionfilter inWindowsCertificateManager.TrustCertificateCoreto detect the cancellation code based on the exception HRESULT’s encoded structure. - Adds inline commentary explaining how the HRESULT is interpreted for this check.
Comment on lines
+94
to
+96
| // https://msdn.microsoft.com/library/cc231198.aspx documents how HResult is formed. | ||
| // In short, the error code is only the 2 least significant bytes. | ||
| catch (CryptographicException exception) when ((exception.HResult & 0xFFFF) == UserCancelledErrorCode) |
Comment on lines
+94
to
99
| // https://msdn.microsoft.com/library/cc231198.aspx documents how HResult is formed. | ||
| // In short, the error code is only the 2 least significant bytes. | ||
| catch (CryptographicException exception) when ((exception.HResult & 0xFFFF) == UserCancelledErrorCode) | ||
| { | ||
| Log.WindowsCertificateTrustCanceled(); | ||
| throw new UserCancelledTrustException(); |
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.
WindowsCertificateManager.TrustCertificateCorewas comparingexception.HResultagainst the raw Win32 error code (ERROR_CANCELLED = 1223 = 0x4C7) instead of its HRESULT-encoded form. SinceHResultusesHRESULT_FROM_WIN32encoding (0x80070000 | win32code), the check never matched, causing user cancellations to surface as unhandled exceptions rather than being caught and converted toUserCancelledTrustException.Description
UserCancelledErrorCodefrom1223tounchecked((int)0x800704C7)— the HRESULT encoding ofERROR_CANCELLEDNote:
Exception.HResultalways returns the HRESULT-encoded value (0x8007xxxxfor Win32 errors), never the raw Win32 error code. The original value1223was therefore dead code — it could never matchexception.HResult. Replacing it withunchecked((int)0x800704C7)is the correct fix; retaining1223alongside the new value would leave unreachable dead code.