silentpartnersoftware / Keycloak.Net

C# client for Keycloak version 17+
MIT License
51 stars 31 forks source link

[Question] Most efficient way of fetching users with roles? #44

Closed PatriQ94 closed 5 months ago

PatriQ94 commented 5 months ago

As far as I know, the GetUsersAsync does not return realm roles assigned to the user. Assuming I have 100 users and 3 roles, which are assigned to these users (1 user can have 1, 2 or all roles). What would be the most efficient way to fetch the list of users with all their roles?

sps-campbellwray commented 5 months ago

Hi @PatriQ94,

Keycloak.Net is not directly associated with Keycloak, we simply provide a library for interacting with Keycloak from a .NET application, so unfortunately I don't know for sure what the best approach here is. I think you would probably be better off directing your question at the wider Keycloak community to see which API endpoint would work best, and from there checking if Keycloak.Net currently implements that endpoint, if not it can be added.

With that said, if you do only have 3 roles, then my best guess would be to use the GetUsersWithRoleNameAsync() endpoint, which would allow you to get the list of users with each role. This would only require 3 calls to the Keycloak API, you could then combine this information with the list of users to give you a list of users and their assigned roles. This will likely be more efficient than looping through each user to fine their roles (which in your example would require 100 calls to the API).

Good luck with your project!

Thanks, Campbell

PatriQ94 commented 5 months ago

Hey Campbell! Thank you for your answer, that's the exact solution we had in mind so far :D