Repository navigation
Cancel pending cookie renewal when principal validation fails - #70028
Open
GrantTotinov wants to merge 2 commits into
Open
GrantTotinov wants to merge 2 commits into
GrantTotinov wants to merge 2 commits into
Conversation
CookieAuthenticationHandler runs the sliding expiration check before ValidatePrincipal so that the refresh ticket is cloned without any per-request changes to the principal. When ValidatePrincipal then rejected the principal, HandleAuthenticateAsync returned NoPrincipal but kept the pending refresh. Unless sign-in or sign-out was also called, FinishResponseAsync still wrote a renewed cookie for the rejected ticket and, with a session store, called ITicketStore.RenewAsync for it. Clear the pending refresh state when validation leaves no principal. The snapshot taken before validation and the successful path are unchanged, and RejectPrincipal still doesn't sign out, so the existing cookie and stored session keep their original expiration. Fixes dotnet#69701
GrantTotinov
requested review from
Youssef1313,
cincuranet,
davpetr,
halter73 and
rokonec
as code owners
October 24, 2026 01:19
Contributor
|
Thanks for your PR, @GrantTotinov. Someone from the team will get assigned to your PR shortly and we'll get it reviewed. |
Contributor
|
Looks like this PR hasn't been active for some time and the codebase could have been changed in the meantime. |
If ValidatePrincipal threw, for example on a transient store failure in SecurityStampValidator before it rejects and signs out, the refresh requested by CheckForRefreshAsync was left pending. When exception handling middleware such as UseExceptionHandler then wrote an error response, FinishResponseAsync renewed the cookie and, with a session store, called ITicketStore.RenewAsync, moving IssuedUtc forward. Move the reset into a CancelRefresh helper and also call it from a catch around ValidatePrincipal before rethrowing.
Open
8 tasks
This branch was previously deployed
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.
Cancel pending cookie renewal when principal validation fails
Don't renew the cookie when ValidatePrincipal rejects the principal or throws
Description
CookieAuthenticationHandler.HandleAuthenticateAsyncruns the sliding expiration check beforeValidatePrincipal, so the refresh ticket is cloned before any per-request changes to the principal. That pending refresh was kept in two cases where validation didn't succeed:ValidatePrincipalrejected the principal. The handler returnedNoPrincipal, but unless sign-in or sign-out was also called in that request,FinishResponseAsyncstill wrote a renewed cookie for the rejected ticket, and with aSessionStoreit also calledITicketStore.RenewAsyncfor it.ValidatePrincipalthrew, for example on a transient store failure inSecurityStampValidatorbefore it reachesRejectPrincipal()andSignOutAsync(). If exception handling middleware such asUseExceptionHandlerthen wrote an error response,FinishResponseAsyncstill ran when that response started and renewed the ticket the same way, movingIssuedUtcforward.Changes:
CancelRefresh()clears the pending refresh (_shouldRefresh,_refreshTicketand the two refresh timestamps).HandleAuthenticateAsynccalls it before returningNoPrincipalfor a rejected principal, and in acatcharoundEvents.ValidatePrincipal(context)before rethrowing.SignOutAsyncstill deletes the cookie and removes the stored session.Tests:
CookieTests.CookieNotRenewedWhenRejectedByValidator, for both cookie-only and session store configurations with sliding expiration on. It rejects once before a renewal is due, once after it's due (noSet-Cookie, stored ticket not replaced), and then rejects and signs out (deletion cookie, stored session removed).CookieTests.CookieNotRenewedWhenValidatorThrows, for the same two configurations.ValidatePrincipalthrows andUseExceptionHandlerwrites a 500 response. It throws once before a renewal is due and once after it's due (noSet-Cookie, stored ticket not replaced), and then a request where validation succeeds still gets the renewal.RenewAsyncalso replaces the stored ticket with one that has a laterIssuedUtcandExpiresUtc.Microsoft.AspNetCore.Authentication.Test(716 passed, 3 skipped in source) andMicrosoft.AspNetCore.Identity.InMemory.Test(226 passed) pass locally.Compatibility
The only observable change is that the response for a principal that was rejected, or whose validation threw, no longer carries a renewed cookie or triggers
RenewAsync. There's no public API change, and the ticket format and data protection purposes are the same, so existing cookies and stored tickets keep working.SecurityStampValidatoralready signs out when the stamp check fails, so the usual Identity flow behaves the same. WhenSecurityStampValidatorOptions.OnRefreshingPrincipalsetsNewPrincipalto null, authentication fails without a sign-out, and that path now skips the renewal as well.I only targeted main. Whether this should go to release/8.0 or release/10.0 is a servicing call, so I've left that to you. Happy to file a breaking change note if you think it needs one.
Fixes #69701