Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to check whether user has any role?

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading