Skip to content

Commit 46cdb51

Browse files
authored
Merge pull request #9341 from abpframework/maliming/SecurityStampValidatorCallback
Add SecurityStampValidatorCallback to Identity module.
2 parents 8bebad2 + ad852e9 commit 46cdb51

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System.Linq;
2+
using System.Threading.Tasks;
3+
using Microsoft.AspNetCore.Identity;
4+
5+
namespace Volo.Abp.Identity.AspNetCore
6+
{
7+
public class AbpSecurityStampValidatorCallback
8+
{
9+
/// <summary>
10+
/// Implements callback for SecurityStampValidator's OnRefreshingPrincipal event.
11+
/// https://github.com/IdentityServer/IdentityServer4/blob/main/src/AspNetIdentity/src/SecurityStampValidatorCallback.cs
12+
/// </summary>
13+
public class SecurityStampValidatorCallback
14+
{
15+
/// <summary>
16+
/// Maintains the claims captured at login time that are not being created by ASP.NET Identity.
17+
/// This is needed to preserve claims such as idp, auth_time, amr.
18+
/// </summary>
19+
/// <param name="context">The context.</param>
20+
/// <returns></returns>
21+
public static Task UpdatePrincipal(SecurityStampRefreshingPrincipalContext context)
22+
{
23+
var newClaimTypes = context.NewPrincipal.Claims.Select(x => x.Type).ToArray();
24+
var currentClaimsToKeep = context.CurrentPrincipal.Claims.Where(x => !newClaimTypes.Contains(x.Type)).ToArray();
25+
26+
var id = context.NewPrincipal.Identities.First();
27+
id.AddClaims(currentClaimsToKeep);
28+
29+
return Task.CompletedTask;
30+
}
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)