I'm trying to add custom claims to the logging in user principal using the OnTokenValidated event. To get these claims I need to fetch data from the database.
The event is called, I can access the database and everything should be fine:
options.Events.OnTokenValidated = requestContext=> {
var ctx = requestContext.HttpContext.RequestServices.GetRequiredService<IPersistenceContext>();
var principal = requestContext.Principal;
var userExternalId = principal.FindFirstValue(IdentityModel.JwtClaimTypes.Subject);
var user = await ctx.Users
.FirstOrDefaultAsync(u => u.ExternalId == userExternalId);
//do stuff with the user
}
Except, in the OnTokenValidated I would need to access claims that are mapped using ClaimActions.MapJsonKey. For example, I would need to access the user's roles, mapped like this:
options.ClaimActions.MapJsonKey(JwtClaimTypes.Role, JwtClaimTypes.Role, JwtClaimTypes.Role);
Now when I try to check the principal's claims in the OnTokenValidated, there are no role claims. Only the 'standard' OIDC claims are present.
The roles are available to the application after the login has completed, so their mapping works as expected.
How can I access the role claims (and anything else mapped via ClaimActions.MapJsonKey) in an OpenIdConnectEvent?
question from:
https://stackoverflow.com/questions/65918730/aspnetcore-3-1-openidconnectevent-using-claims-from-claimactions-mapjsonkey 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…