I just did it for the first time today. There are two main steps here.
First: Create a class that implements the IClaimsTransformer interface.
public class MyTransformer : IClaimsTransformer
{
public Task<ClaimsPrincipal> TransformAsync(ClaimsTransformationContext context )
{
if(context.Principal.Identity.IsAuthenticated)
{
((ClaimsIdentity)context.Principal.Identity)?.AddClaims(...);
}
}
return Task.FromResult(context.Principal);
}
Second: Add this line to Startup.cs in
public void Configure(IApplicationBuilder app, ..., ...)
{
app.UseOpenIdConnectAuthentication( new OpenIdOptions
{
});
app.UseClaimsTransformation(o => new MyTransformer().TransformAsync(o));
app.UseMvc(...);
}
Keep in mind that TransformAsync will run on every request, so you may need to examine sessions or caching if you hit the database with it.
source
share