Claims transformation support missing in ASP.NET Core 2.0

terjetyl picture terjetyl · Aug 16, 2017 · Viewed 8.6k times · Source

I am using JWT Bearer auth in my new asp.net core 2.0 api app and want to add some extra claims to the current identity. This extra info is located in another api which need to be queried. My understanding is that claims transformation would be the proper place to do this. In .net core 1.1 you have the IClaimsTransformer interface in Microsoft.AspNetCore.Authentication nuget package, but I cannot install this one in my .net core 2.0 app. Is there a alternative way to transform claims in asp.net core 2.0 and is this the correct approach for my use case?

Answer

Canada Wan picture Canada Wan · Jan 11, 2018

IClaimsTransformer has been renamed to IClaimsTransformation in ASP.NET Core 2.0.

Claims Transformation Simpler, new IClaimsTransformation service with a single method: Task TransformAsync(ClaimsPrincipal principal) We call this on any successful AuthenticateAsync call.

services.AddSingleton<IClaimsTransformation, ClaimsTransformer>();

private class ClaimsTransformer : IClaimsTransformation {
    // Can consume services from DI as needed, including scoped DbContexts
    public ClaimsTransformer(IHttpContextAccessor httpAccessor) { }
    public Task<ClaimsPrincipal> TransformAsync(ClaimsPrincipal p) {
        p.AddIdentity(new ClaimsIdentity());
        return Task.FromResult(p);
    }
}

See https://github.com/aspnet/Security/issues/1310