Open Closed

Adding Custom Claims to the Logged in User #1190


User avatar
0
Mohammad created

Hello

I am trying to add custom claims to the logged in user. The claims are getting added and can be viewed after the user has logged in. However when I try to get the claim value through a microservice it doesn't exists in the CurrentUser.

I have used the below example to provide custom claims.

public class MyClaimsPrincipalContributor: IAbpClaimsPrincipalContributor, ITransientDependency
{
    public async Task ContributeAsync(AbpClaimsPrincipalContributorContext context)
    {
        var currentTenant = context.ServiceProvider.GetRequiredService<ICurrentTenant>();
        if (currentTenant.Id != null)
        {
            var tenantStore = context.ServiceProvider.GetRequiredService<ITenantStore>();
            var tenant = await tenantStore.FindAsync(tenantId.Value);
            var claimsIdentity = new ClaimsIdentity();
            claimsIdentity.AddIfNotContains(new Claim("tenantname", tenant.Name));
            context.ClaimsPrincipal.AddIdentity(claimsIdentity);
        }
    }
}

Check the docs before asking a question: https://docs.abp.io/en/commercial/latest/ Check the samples, to see the basic tasks: https://docs.abp.io/en/commercial/latest/samples/index The exact solution to your question may have been answered before, please use the search on the homepage.

  • ABP Framework version: v4.2.2
  • UI type: Angular
  • DB provider: EF Core
  • Tiered (MVC) or Identity Server Separated (Angular): yes
  • Exception message and stack trace:
  • Steps to reproduce the issue:

4 Answer(s)
  • User Avatar
    1
    maliming created
    Support Team Fullstack Developer

    hi

    when I try to get the claim value through a microservice it doesn't exists in the CurrentUser.

    Can you check the claims in your jwt(https://jwt.io/)? You can also try the following configuration.

    Configure<AbpClaimsServiceOptions>(options=>
    {
        options.RequestedClaims.Add("tenantname")
    })
    
  • User Avatar
    0
    Mohammad created

    I am not able to find AbpClaimsServiceOptions. I am using v4.2.2. Which Package should I use to configure these options?

    here is the jwt token. It doesnt contain the custom claim

    {
      "nbf": 1618478761,
      "exp": 1650014761,
      "iss": "https://localhost:44374",
      "aud": [
        "ResellerManagementService",
        "ResellerBackendGateway"
      ],
      "client_id": "reseller-management",
      "sub": "39fb4d11-7a81-1199-d025-822914236ebf",
      "auth_time": 1618478759,
      "idp": "local",
      "preferred_username": "admin@zed.com",
      "phone_number_verified": "False",
      "email": "admin@zed.com",
      "email_verified": "False",
      "name": "admin@zed.com",
      "sid": "23DAB653DBF2AE65AD44614FC6B32290",
      "iat": 1618478761,
      "scope": [
        "openid",
        "profile",
        "ResellerBackendGateway",
        "ResellerManagementService"
      ],
      "amr": [
        "pwd"
      ]
    }
    
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi please check this https://github.com/abpframework/abp/issues/8073#issuecomment-799999030

  • User Avatar
    0
    Mohammad created

    @maliming Thanks. It worked.

Made with ❤️ on ABP v9.1.0-preview. Updated on December 12, 2024, 07:15