How to get user id, and user UUID/GUID from Keycloak?

I’m using Keycloak inside docker and hosted on https://accounts.example.com.

I’m using React as my client library and after login, the user goes into https://user.example.com.

This React client uses .NET Core API located at https://api.user.example.com.

I use keycloak.js to manage access in React panel.

Now let’s say the user wants to create a ticket. I send this ticket info to the API:

{
    title: 'Urgent backup',
    priority: 'urgent',
    description: 'Please backup my files immediately'
}

In our database, we have this design:

create table Tickets
(
    Id bigint not null primary identity(1, 1),
    UserGuid uniqueidentifier not null, -- How should I fill this column?
    Title nvarchar(200) not null,
    PriorityId int not null,
    Description nvarchar(max) not null
)

The problem is, API should extract permanent data from the user.

I can’t store email or username in that column of course. And I can’t even rely on username or email because Keycloak’s admin panel allows this setting to be configured:

enter image description here

Thus the best option is to give the user a GUID or UUID in Keycloak and retrieve it in open id token.

I searched but I can’t find how to do this.

Can you help?

I guess you can use sub claim value from the access token - it should contain Keycloak user uuid. Of course verify it first - you may have different setup.

1 Like