asp.net-identityidentityserver4identityclaims.net-core-2.2

Inject custom claims from client after login using identity server 4


We are using identity server 4 and consuming it using web application. We are able to successfully login and add some extra claims from the ID4 server using profile service but we want add some more claims from the client end.

Basically our login request goes to ID4 server and it validates and sends back token to the client application. Now in client application user will selects the company,position ,financial year from client application.

We want to add the selected details from below screen to token as claims.

Is it possible ??

Any help with sample code is appreciated :)

enter image description here


Solution

  • By using IClaimsTransformation we can achieve it. It will be triggered for every request in the application.

    public class CustomTransformation : IClaimsTransformation
    {
        private readonly IHttpContextAccessor contextAccessor;
            public CustomTransformation (IHttpContextAccessor context)
        {
            contextAccessor = context;
        }
    
        public Task<ClaimsPrincipal> TransformAsync(ClaimsPrincipal principal)
        {
             return Task.FromResult(principal);
        }
    }