Unable to find Roles For CurrentUser????

Posted by Community Admin on 04-Aug-2018 20:04

Unable to find Roles For CurrentUser????

All Replies

Posted by Community Admin on 07-May-2013 00:00

I am trying to dynamically allow functionality of inserts/edits/deletes in a user control calender widget based on who is logged in, but I am unable to find the roles for the current user?

I am trying to allow certain groups certian functionality, but without finding the roles, this is useless...

...
var identity = ClaimsManager.GetCurrentIdentity();
RoleManager roleManager = RoleManager.GetManager();
EventsManager manager = EventsManager.GetManager();

//default radscheduler function 
Calendar.AllowDelete = false;
Calendar.AllowEdit = false;
Calendar.AllowInsert = false;

if (identity.IsAuthenticated)

List<Role> roles = roleManager.GetRolesForUser(ClaimsManager.GetCurrentUserId()).ToList();
labeltext += " Found " + roles.Count().ToString() + " roles for " + ClaimsManager.GetCurrentUserId);
//labeltext = identity.Name + " is Authenticated as ID " + identity.UserId;
var isUserInRole = roleManager.IsUserInRole(identity.UserId, "Authors");

if (isUserInRole)

labeltext = labeltext + " + is in Authors";
Calendar.AllowInsert = true;


isUserInRole = roleManager.IsUserInRole(identity.UserId, "Editors");

if (isUserInRole)

labeltext = labeltext + " + is in Editors";
Calendar.AllowDelete = true;
Calendar.AllowEdit = true;
Calendar.AllowInsert = true;


isUserInRole = roleManager.IsUserInRole(identity.UserId, "Administrators");

if (isUserInRole)

labeltext = labeltext + " + is in Administrators";
Calendar.AllowDelete = true;
Calendar.AllowEdit = true;
Calendar.AllowInsert = true;



but if I log in for an admin account I will get the labeltext... as seen in the attached and below

Found 0 roles for dc1f1f74-64d1-4fa2-89ff-b0e0f71a00ba

I can login to the backed in and for this user_id  i see roles for admin...and a test user I see three different roles... I can even log into the sql tables and run a query and find the roles... (see attached)

am I missing something?  I tried following the documentation for this link...
www.sitefinity.com/.../users-and-roles

Posted by Community Admin on 10-May-2013 00:00

Hi Dustin,

You are not able to find the roles for the current user, because you are searching them in the wrong role provider: RoleManager.GetManager() returns manager for the default role provider. However, the roles like Administrators, Authors, Editors, etc belongs to AppRoles provider (They are in the same table sf_roles but with different app_name). You can check the available role providers from Administration > Settings > Advanced > Security > Role Providers.

In order to get correct results, you have to get the role manager by specifying the AppRoles provider:

RoleManager roleManager = RoleManager.GetManager("AppRoles");

However, much better approach is to use the cached collection of roles in the current identity. There is no need to query the database every time, since you already have this information cached. Here is an example:
var user = ClaimsManager.GetCurrentIdentity();
var isAuthenticated = user.IsAuthenticated;
var isBackend = user.IsBackendUser;
var isUnrestricted = user.IsUnrestricted;
var isAuthor = user.Roles.Any(r => r.Name == "Authors" && r.Provider == "AppRoles");

Hope this is helpful.

Regards,
Vlad
the Telerik team
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

This thread is closed