-
Notifications
You must be signed in to change notification settings - Fork 599
[Regression] Role/claim changes in identity aren't persisted into the auth cookie #1788
Comments
@Tratcher here you go. I still think the problem is that Identity modifies |
This test doesn't cover the case where ctx.ReplacePrincipal() is used to replace the principal with a new one, since you're using the reference directly. |
As a quick and dirty workaround, this seems to work options.OnRefreshingPrincipal = context =>
{
// Any original code in OnRefreshingPrincipal
// ...
// Copy the new principal claims into the old object while keeping the reference to context.ticket.Principal
var oldIdentity = context.CurrentPrincipal.Identities.First();
var newIdentity = context.NewPrincipal.Identities.First();
Claim claim;
while ((claim = oldIdentity.Claims.FirstOrDefault()) != null)
{
oldIdentity.RemoveClaim(claim);
}
foreach (var newClaim in newIdentity.Claims)
{
oldIdentity.AddClaim(newClaim);
}
return Task.FromResult(0);
}; |
@HaoK can you take a look? If this regressed then it's a patch candidate. |
Sure, I wouldn't be surprised if this was a regression from the cloning change |
Well so this is interesting, looks like the security stamp might have been relying on the old behavior before to persist updates to the cookie: See https://github.com/aspnet/Identity/blob/dev/src/Identity/SecurityStampValidator.cs#L80 Expectation was that ReplacePrincipal + ShouldRenew would cause the cookie to be updated for the user. I'll add a functional test in identity confirming that this is indeed broken now |
This is the regression change with the cloning, might need to just revert? 1df139e Since it does change the semantics of ReplacePrincipal (which Identity's SecurityStampValidator was relying on) |
@HaoK Just wondering here, why not keep a reference to the ticket instead and set up |
So certainly we can work around the new behavior in identity, but if this broke identity, it should at least be looked at like a regression. |
Or alternatively, alter ReplacePrincipal to also set the ticket's principal. |
That's true |
@kanadaj may be right, we should be able to fix ReplacePrincipal to allow explicit cookie updates and still clone to prevent later app contamination. |
@muratg this needs triaging. It's a regression in 2.1 and may warrant a patch. |
cc @Eilon |
Patch is approved. |
@HaoK - are we good to close this bug now? Is the fix in the patch and in 2.2 and in master? |
Yep, thx for reminding me, fixed via #1811 |
When ASP.NET Core Identity tries to update the current user's claims, it updates the Principal of the current request, however the update to the claims is not persisted to the cookie.
I've also made an issue for this in aspnet/Identity before I've managed to figure out the details: aspnet/Identity#1845
I'm not sure which repo this issue really belongs to considering this seems like an interop issue.
Repro repo with steps: https://github.com/kanadaj/aspnetrefreshrolesrepro/tree/master
The text was updated successfully, but these errors were encountered: