Is it possible to set a boolean-value claims in your Identity Token with Duende Identity Server 5?

762 views Asked by At

I'm trying to set a custom claim with some code for Duende Identity Server 5.2.3.

The claim works / is added, but it's a string and not a boolean. I've notied -another- claim inthe JWT that is a boolean so I'm wondering, can I do this also?

Here's the code and then the sample JWT:

public class CustomTokenService : DefaultTokenService
{
    public override async Task<Token> CreateIdentityTokenAsync(TokenCreationRequest request)
    {
        var token = await base.CreateIdentityTokenAsync(request);

        bool isThisInAGracePeriod = true; // for example ...

        // This doesn't work. There's no bool overload, for the 2nd argument.
        // var myClaim = new Claims("in_grace_period", isThisInAGracePeriod);

        // I need to convert the bool to a string, using ToString();
        var myClaim = new Claims("in_grace_period", isThisInAGracePeriod.ToString());

        token.Claims.Add(myClaim);
    }
}

enter image description here

so notice:

  • email_verified is a bool value
  • in_grace_period is a string value (because I had to ToString() it :( )

Is it possible to add my custom claim as a bool so it ends up looking like how email_verified is serialized out to the token payload?

1

There are 1 answers

0
Dimitris Maragkos On BEST ANSWER

Yes, Claim class has a constructor that accepts 3 parameters and the 3rd one is value type.

var claim = new Claim(
    type: "in_grace_period", 
    value: isThisInAGracePeriod.ToString().ToLower(),
    valueType: ClaimValueTypes.Boolean);