Home > Mobile >  How to check whether user has any role?
How to check whether user has any role?

Time:01-03

I'm currently facing an issue where I need to check if any role has been assigned to a user. The UserManager.IsInRoleAsync() requires a parameter that specifies the role name I want to check. There's a way I can get which is fetch all roles and check one by one using for loop but it does not seem ideal to. Is there any ideal way to do this?

Thank you.

CodePudding user response:

You can get all the roles assigned to a user, and check if there's any. There's no need for a for loop, you just want to know that it's not empty.

var roles = await _userManager.GetRolesAsync(user);
var hasAnyRole = roles.Count > 0;
  • Related