Skip to content

VCST-6498: Reset the email confirmation on an email change - #154

Merged
DmitryGrishinVirtoworks merged 10 commits into
devfrom
feature/VCST-6498
Nov 24, 2026
Merged

DmitryGrishinVirtoworks merged 10 commits into
devfrom
feature/VCST-6498

Conversation

@DmitryGrishinVirtoworks

@DmitryGrishinVirtoworks DmitryGrishinVirtoworks commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Description

On dev, updatePersonalData and updateUser reset the confirmation of a changed email (#153 and #155, VCST-6970) but keep the security stamp, so the MFA codes and two-factor tokens issued before the change stay valid.

Now both save a changed email with UserManager.SetEmailAsync: the new email is not confirmed, and the security stamp changes. updateUser does it in the same write as the phone number and the photo. A change of letter case only is saved as is and keeps the confirmation and the stamp, as the Backoffice compares the email without case. The Backoffice itself keeps the old stamp on an email change; that is outside this PR.

No schema change, no migrations, no new settings, no new dependencies.

Companion PR: VirtoCommerce/vc-module-mfa#9 — the MFA code is sent to an unconfirmed email and confirms it. The access rules and the protected fields of updateUser come from #153 and #155; this PR changes only how a changed email is saved.

New GraphQL queries and mutations

None. This PR adds no query or mutation.

Changes to existing GraphQL queries and mutations

  • updatePersonalData: a changed email also changes the security stamp; a change of letter case only keeps the confirmation (dev resets it). The input and the result are unchanged.
  • updateUser: a changed email also changes the security stamp; a change of letter case only is saved and keeps the confirmation (dev drops it). The input and the result are unchanged.

New public services / interfaces / methods

None.

New protected methods (extensibility)

  • UserCommandHandlerBase.SaveUserAsync(UserManager<ApplicationUser> userManager, ApplicationUser user, string storedEmail) — saves the user of updatePersonalData and updateUser: a changed email through SetEmailAsync, otherwise through UpdateAsync. Override it to change how both mutations save an email change.

Breaking changes

  • Compile: none. No public member or signature changed.
  • Package upgrade: none.
  • Runtime behaviour:
    • updatePersonalData and updateUser change the security stamp when the email changes; a change of letter case only keeps the confirmation and the stamp, and updateUser now saves it (before, it was dropped).
    • The new stamp ends the user's Backoffice sessions at their next validation (Authorization:SecurityStampValidationInterval) and the sign-ins waiting for a second factor, and invalidates the password reset and email confirmation links sent before the change. It doesn't end storefront sessions: access tokens don't carry the stamp, and the refresh token grant doesn't check it.
    • A changed email is saved through UserManager.SetEmailAsync, which calls the protected UpdateUserAsync, not UpdateAsync: a UserManager subclass that overrides UpdateAsync no longer sees these saves, and roles and logins aren't synchronized on this path. The user events are the same.
    • GetUserWithProfileChangesOnly no longer resets EmailConfirmed, and it now returns a change of letter case only. On a changed email, SetEmailAsync sets the confirmation and the stamp whatever an override returns.

New dependencies

None.

Note (what the email reset does not cover): the storefront still changes the email without asking for the password or a code, so whoever holds a user's session can change the email and receive the next MFA code there. This was already possible before this PR; a storefront session also survives the new stamp, see above. Closing it needs a confirmation of the email change itself, which VCST-6971 covers.

References

QA-test:

Jira-link:

https://virtocommerce.atlassian.net/browse/VCST-6498

Artifact URL:

https://vc3prerelease.blob.core.windows.net/packages/VirtoCommerce.ProfileExperienceApiModule_3.1028.0-pr-154-1a78.zip
🤖 Generated with Claude Code

…ateUser to the phone and the photo

updatePersonalData sets the email with UserManager.SetEmailAsync, so the new email is not
confirmed and the security stamp changes. updateUser loads the user and changes only the phone
number and the photo URL; the other fields of its input are ignored and marked as deprecated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ateUser in one write

updatePersonalData compares the email without case, as the Backoffice does: a change of letter
case only keeps the confirmation. updateUser saves the phone number and the photo URL in one write
and treats an empty value as no value.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
dev now has the updateUser fix of VCST-6970 (#153), so this branch takes its handler and tests
and drops its own updateUser changes: the handler, the deprecated input fields, the schema
sample, the tests and the README paragraph. Only the updatePersonalData change remains.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@DmitryGrishinVirtoworks DmitryGrishinVirtoworks changed the title VCST-6498: Reset the email confirmation on email change and limit updateUser to the phone and the photo VCST-6498: Reset the email confirmation on an email change Nov 23, 2026
Without platform:security:update, updateUser now saves a changed email with
UserManager.SetEmailAsync, like updatePersonalData: the email is not confirmed and the security
stamp changes, so the MFA codes and two-factor tokens issued before the change are voided.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Without platform:security:update, updateUser applied the requested email only when it differed
without case, so a change of letter case was dropped. It now saves the new case and keeps the
confirmation and the security stamp, like updatePersonalData.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
updateUser no longer takes the CanUpdateSecurityData path (#155): it always saves the profile fields of the current user, and a changed email still goes through UserManager.SetEmailAsync.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@yuskithedeveloper yuskithedeveloper 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 does what the decision in VCST-6498 asks for the profile API. A real email change now goes through UserManager.SetEmailAsync, so the new address is unconfirmed and the security stamp changes. A change of letter case only keeps both. I found no code defect; the findings are about the description and the extension points.

Corroboration N is how many of my three review passes (/simplify, /vc-module-review, /review-pr-virto) reported the finding.

ID Severity Corroboration Finding
F1 minor 3 The description is written against the base before #155 (below)
F2 minor 2 "As the Backoffice does … the security stamp changes" (inline)
F3 minor 2 An email change no longer goes through UserManager.UpdateAsync (inline)
F4 nit 1 The released GetUserWithProfileChangesOnly changes meaning (inline)
F5 nit 1 The email-save rule is written twice (inline)
F6 nit 1 "Tokens … stay valid until they expire" undersells it (below)
F7 nit 2 Test cleanups (inline)
F8 nit 2 A test assert that now describes the input (below)
F9 nit 1 No refused-email test for updateUser (below)
F10 nit 1 README placement and scope (inline)

F1 · minor · corroboration 3: the description reads as if updatePersonalData still kept the confirmation. It says the new email was "saved … as is: the email stayed confirmed", and it lists "a new email is saved as unconfirmed" under the GraphQL changes and the runtime breaks. #155 already resets the confirmation on dev (user.EmailConfirmed = false, L52-L53).

Against the base, this PR does the following:

  • updatePersonalData: a real change renews the stamp, and a case-only change keeps the confirmation (dev resets it).
  • updateUser: a real change renews the stamp, and a case-only change is saved (dev drops it).

I'd rewrite the description from the diff and cite #153 and #155 once.

F6 · nit · corroboration 1: "access and refresh tokens already issued stay valid until they expire" (L80) is right, but it undersells two things, and both are worth a word, since the stamp change is the point of this PR:

  • Refresh tokens roll, and the refresh grant doesn't check the stamp (L314-L339). So a storefront session, a taken-over one included, carries on indefinitely.
  • Password-reset and verification links sent before the change stop working.

F8 · nit · corroboration 2: Handle_WithoutSecurityPermission_KeepsStoredSecurityData changes the email. So it now captures the user passed to the mocked SetEmailAsync, and asserts SecurityStamp == "stored-stamp" (L82). Production then saves a new stamp. What the test guards is that the request's stamp isn't applied, and NotBe("attacker-stamp") says exactly that.

F9 · nit · corroboration 1: updatePersonalData has a refused-email test (Handle_RefusedEmail_ReturnsError), but updateUser doesn't. A test of the same shape would do.

Follow-ups outside both PRs, including the Backoffice side of F2, are proposed in VCST-6498, so there's nothing to do for them here.

Checked and fine

  • Case handling is the same in both handlers and in the Backoffice, and it's null-safe. A refused email returns the Identity errors.
  • updateUser saves the email, phone and photo in one write, since Patch copies all of them.
  • ContactAggregate.UpdatePersonalDetails sets only the names, so a refused email can't leave the contact out of step with the account.
  • Only these two handlers change an existing user's email in this module.
  • The new stamp ends pending MFA sign-ins and mode-change codes, and Backoffice cookie sessions. Access tokens don't carry the stamp.
  • After an email change, a code sent to the old address can no longer confirm the new one, given VirtoCommerce/vc-module-mfa#9's ConfirmEmailAsync.
  • Every new test fails on the base, and they check the right interaction (SetEmailAsync called).
  • There's no schema change and no migration, and the build has no warnings.

Comment thread README.md Outdated

### User accounts

`updatePersonalData` changes the email of the current user with `UserManager.SetEmailAsync`, as the Backoffice does: the new email is not confirmed, and the security stamp changes. A change of letter case only is saved as is and keeps the confirmation. A new security stamp ends the Backoffice sessions of the user at their next validation (`Authorization:SecurityStampValidationInterval`) and the sign-ins waiting for a second factor; access and refresh tokens already issued stay valid until they expire.

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.

F2 · minor · corroboration 2: "as the Backoffice does … the security stamp changes" isn't what the Backoffice ends up doing. SecurityController.Update calls SetEmailAsync, then puts back the stamp it read before and saves again (ReturnPasswordHash is false by default). So a Backoffice email change keeps the old stamp (L926-L954).

The comparison holds for the confirmation and the case handling. For the stamp, this PR does what the decision asked, and the Backoffice doesn't. The description has the same sentence. The platform side is in the Jira follow-ups.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 12cf886: the README and the description no longer say "as the Backoffice does" for the stamp; they say the Backoffice keeps the old stamp. The platform side is a follow-up in VCST-6498.

}
else
{
result = await userManager.SetEmailAsync(user, email);

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.

F3 · minor · corroboration 2: a real email change no longer goes through UserManager.UpdateAsync, because SetEmailAsync calls the protected UpdateUserAsync instead. Two consequences, here and in updateUser:

  • A downstream UserManager / CustomUserManager subclass that overrides UpdateAsync stops seeing storefront email changes.
  • The roles/logins sync doesn't run on this path.

Event subscribers see no difference: the same events fire either way. It isn't under Breaking changes; worth a line under Runtime.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added under Breaking changes → Runtime.

result.EmailConfirmed = false;
}

result.Email = requestedUser.Email;

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.

F4 · nit · corroboration 1: GetUserWithProfileChangesOnly shipped in 3.1026.0 as a protected virtual seam, and this PR changes what it means:

  • It now returns a case-only email change, which it dropped before.
  • On a real change, SetEmailAsync overrides whatever an override returns for EmailConfirmed / SecurityStamp.
  • Roles and logins an override returns aren't synced.

The summary's "security stamp … keeps its stored value" is still true of the method, but not of what gets saved. Probably nobody overrides it yet; one line under Runtime would cover it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added under Breaking changes → Runtime. GetUserWithProfileChangesOnly also no longer resets EmailConfirmed: SetEmailAsync does it.

var userToSave = GetUserWithProfileChangesOnly(storedUser, request.ApplicationUser);

return await userManager.UpdateAsync(userToSave);
return storedUser.Email.EqualsIgnoreCase(userToSave.Email)

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.

F5 · nit · corroboration 1: the same rule ("case-only change → UpdateAsync, otherwise SetEmailAsync") is written here and in UpdatePersonalDataCommandHandler, in two shapes, and GetUserWithProfileChangesOnly decides it a third time. One protected virtual helper on UserCommandHandlerBase, e.g. SaveUserAsync(userManager, user, storedEmail), would hold it. VCST-6971 will need to change exactly this rule.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 12cf886: both handlers save through UserCommandHandlerBase.SaveUserAsync(userManager, user, storedEmail).

}

[Fact]
public async Task Handle_EmailNotChanged_DoesNotSetEmail()

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.

F7 · nit · corroboration 2: some test cleanups.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 12cf886: the email tests are merged with #155's, "EmailNotChanged" sends the same email, the extra blank lines are gone, and UpdatePersonalDataCommandHandlerTests has a UserId const.

Comment thread README.md Outdated
* `errorCode` is the rule: `InvalidCharacters` (name patterns), `HtmlTagsNotAllowed`, `UnsafeContent`, `InvalidEmail` (email format), or a FluentValidation code such as `NotEmptyValidator` or `MaximumLengthValidator`.
* `message` is in English. A client that shows the error to users should translate it by `errorCode`.

### User accounts

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.

F10 · nit · corroboration 1: this section describes behaviour, but it sits under "## Configuration". Also, "updateUser changes the email of the current user" is narrower than the code: an admin changing another user's email takes the same path.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 12cf886: "User accounts" is now its own section, and it says updateUser changes the user's own account, or any account for an administrator.

Both handlers save the user through UserCommandHandlerBase.SaveUserAsync. Add a refused-email test for updateUser and tidy the tests. Move the README section out of Configuration and correct what it says about the Backoffice and the security stamp.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@DmitryGrishinVirtoworks

DmitryGrishinVirtoworks commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks for the review. Fixed in 12cf886, replies are on the inline comments. The rest:

The follow-ups are answered in VCST-6498.

@sonarqubecloud

Copy link
Copy Markdown

@yuskithedeveloper yuskithedeveloper 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.

Thanks, Dmitry. I checked the fixes in 12cf886 and 44564c5 against each finding, and they're all done. SaveUserAsync keeps the rule in one place, the description now matches the diff against dev, and the merged tests pin both routes.

One note, not blocking:

F11 · nit: inline.

result.EmailConfirmed = false;
}

result.Email = requestedUser.Email;

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.

F11 · nit: GetUserWithProfileChangesOnly no longer resets EmailConfirmed on a changed email. The base path is fine, because SaveUserAsync sends any real change to SetEmailAsync.

In 3.1026.0 and 3.1027.0, though, this method's result came with the confirmation already reset. A custom Handle that saved it with UpdateAsync was therefore safe, and now it would keep a changed email confirmed. You've declared the change, and there are probably no such overrides yet, so it's fine as is. Keeping the one-line reset would just keep the safe outcome as their default.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Leaving it as is. The base path is covered by SaveUserAsync, the change is declared under Breaking changes → Runtime, and keeping the reset would decide the rule in two places again (F5). If an override of Handle shows up, the reset can come back then.

@DmitryGrishinVirtoworks
DmitryGrishinVirtoworks merged commit c13119f into dev Nov 24, 2026
12 checks passed
@DmitryGrishinVirtoworks
DmitryGrishinVirtoworks deleted the feature/VCST-6498 branch November 24, 2026 17:40
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.

2 participants