user3766657 user3766657 - 4 months ago 69
ASP.NET (C#) Question

How do you inject a scoped service into an implementation of IClaimsTransformer?

ASP.Net core lets you modify a ClaimsPrincipal using an implementaion of IClaimsTransformer. I understand that you register it like this:

app.UseClaimsTransformation(o => o.Transformer = new MyClaimsTransformer());

But inside MyClaimsTransformer I need access to my database. My question is how can I inject or get access to a scoped service that provides that data access inside of MyClaimsTransformer?


I think it is not possible at the moment.

Implementation of the method you're using can be found here

public static IApplicationBuilder UseClaimsTransformation(this IApplicationBuilder app, Func<ClaimsPrincipal, Task<ClaimsPrincipal>> transform)
    if (app == null)
        throw new ArgumentNullException(nameof(app));
    if (transform == null)
        throw new ArgumentNullException(nameof(transform));

    return app.UseClaimsTransformation(new ClaimsTransformationOptions 
        Transformer = new ClaimsTransformer { OnTransform = transform }

As you can see, it is a simple proxy method for providing ClaimsTransformationOptions only property Transformer.

And when you get only ClaimsPrincipal, you cannot reach request from it.

It might have been you, who submitted this issue, but if it was not, you can track it here.