
Often you have the requirement that you need or want to add additional claims to a token - either the, ID token or the access token. This is especially useful when one needs an additional custom enrichment that is not available at the time of token creation.
ASP.NET Core has a corresponding interface in the Auth pipeline for this purpose: the IClaimsTransformation .
All implementations of this interface are instantiated automatically if they have been registered.
1services.AddScoped<IClaimsTransformation, AddCustomClaimsToIdentityTransformation>();
All implementations are run through in the order in which they were registered.
The implementation itself is very simple:
1public class AddCustomClaimsToIdentityTransformation : IClaimsTransformation
2{
3 public async Task<ClaimsPrincipal> TransformAsync(ClaimsPrincipal principal)
4 {
5 ClaimsPrincipal clone = principal.Clone();
6 ClaimsIdentity newIdentity = (ClaimsIdentity)clone.Identity;
7
8 // support aad / ad / others, too
9 Claim? nameId = principal.Claims
10 .FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier || c.Type == ClaimTypes.Name);
11 if (nameId is null) return principal;
12
13 // request data from your claim source
14 var myClaimData = await _myRepository.GetAdditionClaimsOfUser(nameId).ConfigureAwait(false);
15 foreach (var customClaim in myClaimData)
16 {
17 newIdentity.AddClaim(new(customClaim.Type, customClaim.Value));
18 }
19
20 return clone;
21 }
22}
Related articles

Feb 20, 2026 · 9 min read
.NET 11 Preview 1 Is Here: What's New and What to Expect
The .NET team just released the first preview of .NET 11 and there is already a lot to talk about. While this is an early preview - not a …

Jan 02, 2026 · 4 min read
Build a Custom FeatureGate Attribute in ASP.NET Core with C#
Feature management is a key technique in modern web applications, allowing you to enable or disable features dynamically without redeploying …

Nov 28, 2025 · 5 min read
Custom IFeatureDefinitionProvider: Feature Flags from Tests and Databases
Feature flags are a great way to ship safely, run experiments and keep production changes reversible. In .NET, the de-facto standard is …
Let's Work Together
Looking for an experienced Platform Architect or Engineer for your next project? Whether it's cloud migration, platform modernization or building new solutions from scratch - I'm here to help you succeed.

Comments