Skip to content

Surface failed updates from the Identity API /manage/2fa endpoint - #69966

Open
GrantTotinov wants to merge 1 commit into
dotnet:mainfrom
GrantTotinov:fix-identity-2fa-result-69941
Open

GrantTotinov wants to merge 1 commit into
dotnet:mainfrom
GrantTotinov:fix-identity-2fa-result-69941

Conversation

@GrantTotinov

Copy link
Copy Markdown
Contributor

Surface failed updates from the Identity API /manage/2fa endpoint

  • You've read the Contributor Guide and Code of Conduct.
  • You've included unit or integration tests for your change, where applicable.
  • You've included inline docs for your change, where applicable.
  • There's an open issue for the PR that you are making. If you'd like to propose a new feature or change, please open an issue to discuss the change or find an existing issue.

Description

The /manage/2fa handler registered by MapIdentityApi ignored the IdentityResult returned by SetTwoFactorEnabledAsync and ResetAuthenticatorKeyAsync, and treated a null result from GenerateNewTwoFactorRecoveryCodesAsync as "no new codes". When the user update failed (for example an EF Core optimistic concurrency conflict caused by a concurrent update to the same user), the tracked user entity still held the modified values, so the endpoint returned HTTP 200 with a shared key, 2FA state or recovery codes that were never persisted.

Changes in IdentityApiEndpointRouteBuilderExtensions:

  • Every SetTwoFactorEnabledAsync and ResetAuthenticatorKeyAsync call is checked, including the reset that initializes a missing authenticator key. A failure is returned as a validation problem built from the IdentityResult (e.g. ConcurrencyFailure), the same way the other endpoints in this file already handle failed results.
  • A null result from GenerateNewTwoFactorRecoveryCodesAsync returns a RecoveryCodeGenerationFailed validation problem instead of HTTP 200 with recoveryCodes: null.

A successful response is now only built after every update in the request has been persisted. The response shape for successful requests is unchanged, and there are no public API changes.

Behavior change to call out: a request that previously got HTTP 200 while its update silently failed now gets HTTP 400. If the request changes more than one setting (for example ResetSharedKey, which disables 2FA and then resets the key), the steps that succeeded before the failure stay saved, the same as before this change. The client is now told that the request failed and can retry it.

Tests

Added TwoFactorReturnsValidationProblemWhenUserUpdateFails to MapIdentityApiTests. It uses a UserManager whose user updates can be made to fail with ConcurrencyFailure, and verifies that:

  • initializing the missing shared key, ResetSharedKey, ResetRecoveryCodes and Enable each return a 400 validation problem while updates fail, and
  • once updates succeed again, the stored shared key, 2FA state and recovery code count are unchanged.

The test fails without the fix (the first request returns 200). All Identity.FunctionalTests pass locally.

Fixes #69941

Check the IdentityResult from SetTwoFactorEnabledAsync and ResetAuthenticatorKeyAsync, and treat a null result from GenerateNewTwoFactorRecoveryCodesAsync as a failure, so the endpoint returns a validation problem instead of HTTP 200 with settings that were not persisted.

Fixes dotnet#69941
Copilot AI balanced review requested due to automatic review settings October 22, 2026 02:48

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@github-actions github-actions Bot added the area-identity Includes: Identity and providers label Oct 22, 2026
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Oct 22, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Thanks for your PR, @GrantTotinov. Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Looks like this PR hasn't been active for some time and the codebase could have been changed in the meantime.
To make sure no conflicting changes have occurred, please rerun validation before merging. You can do this by leaving an /azp run comment here (requires commit rights), or by simply closing and reopening.

@dotnet-policy-service dotnet-policy-service Bot added the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Oct 29, 2026
@Youssef1313
Youssef1313 requested review from halter73 and a balanced review from Copilot October 29, 2026 12:44

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.

🟡 Changes recommended

The failure-path test stops before the authenticator-key reset, leaving unverified that a reset failure returns HTTP 400 while preserving the original key and the successfully persisted disabled state.

1 open issue

Review effort: Balanced

Comment on lines +1482 to +1483
await AssertValidationProblemAsync(await client.PostAsJsonAsync("/identity/manage/2fa", new { ResetSharedKey = true }),
"ConcurrencyFailure");

@Youssef1313 Youssef1313 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@halter73 if you want to do a second review pass.

This branch was previously deployed

1 inactive deployment
copilot-pat-pool — 72d7c737 Deployed Oct 22, 2026 by dotnet-policy-service[bot] via pat_pool #1914
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-identity Includes: Identity and providers community-contribution Indicates that the PR has been added by a community member pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Identity API /manage/2fa returns success when a concurrent update prevents saving changes

3 participants