Unable to find Roles For CurrentUser????
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
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"
);
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"
);