-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Maintain claims principal during refresh (#14918)
- Loading branch information
1 parent
75e9e7a
commit a5395f4
Showing
2 changed files
with
36 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
35 changes: 35 additions & 0 deletions
35
src/OrchardCore/OrchardCore.Users.Core/Services/ConfigureSecurityStampOptions.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using System.Linq; | ||
using System.Security.Claims; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Identity; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace OrchardCore.Users.Services; | ||
|
||
public class ConfigureSecurityStampOptions : IPostConfigureOptions<SecurityStampValidatorOptions> | ||
{ | ||
public void PostConfigure(string name, SecurityStampValidatorOptions options) | ||
{ | ||
options.OnRefreshingPrincipal = principalContext => | ||
{ | ||
var currentIdentity = principalContext.CurrentPrincipal?.Identities?.FirstOrDefault(); | ||
if (currentIdentity is not null && principalContext.NewPrincipal.Identities is not null) | ||
{ | ||
var newIdentity = principalContext.NewPrincipal.Identities.First(); | ||
foreach (var claim in currentIdentity.Claims) | ||
{ | ||
if (newIdentity.HasClaim(claim.Type, claim.Value)) | ||
{ | ||
continue; | ||
} | ||
newIdentity.AddClaim(new Claim(claim.Type, claim.Value)); | ||
} | ||
} | ||
return Task.CompletedTask; | ||
}; | ||
} | ||
} |