How to add data (an Array) to jwt token which is generated by Identityserver 4

Viewed 727

can anyone can help me on how to add an array to jwt token of identityserver4 in the format below .

If array contains no data : { "custom_data" : [] }
If array contains one element : { "custom_data" : ["one_element"]}
If array contains multiple elements : { "custom_data" : ["first element","second element"]}

I have tried using IProfileService and added data by using Claims
sample code :

foreach (string element in my_array) {
  userClaims.Add(new Claim("custom_data" , element));
}

But in that case what I'm getting is as follows :

if array contains no data : { } here I'm not getting any data
if array contains one element : { "custom_data" : "one_element"} Here I'm getting it as a string
if array contains multiple elements : { "custom_data" : ["first element","second element"]} Here I'm getting in a correct way

1 Answers

I would check out this post ab follow the advice from the OAuth 2.0 for Browser-Based Apps document:

To avoid the risks inherent in handling OAuth access tokens from a purely browser-based application, implementations may wish to move the authorization code exchange and handling of access and refresh tokens into a backend component.

The backend component essentially becomes a new authorization server for the code running in the browser, issuing its own tokens (e.g. a session cookie). Security of the connection between code running in the browser and this backend component is assumed to utilize browser-level protection mechanisms.

In this scenario, the backend component may be a confidential client which is issued its own client secret.

Related