Issue retrieving user roles via API on an upgraded site
Hi
I've recently upgraded a site from 3.6 to 5.2 and have some legacy custom code that I'm struggling to get working.
Basically to reset a users password on a custom page, the user is deleted and recreated, and then added to the 'Member' role.
To retrieve the user I do the following
UserManager um = Telerik.Sitefinity.Security.UserManager.GetManager("OpenAccessMembership37Provider");
User user = um.GetUser(iMemberNumber.ToString());
This works fine and I can delete and recreate the users without any issues.
The problem is when trying to assign the user to the 'Member' role. The site appears to only have one role I can assign users to (Moderators (forums)). I can see this by calling the GetRoleNames on the role manager.
If I go into site administration though, I can see the user belongs to the 'Members' role as part of the OpenAccessMembership37Provider.
I am guessing that my problem is in the instanciation of the role manager. I use GetManager() with no parameter passed in. Passing in the OpenAccessMembership37Provider string (like I do for the user manager) throws an error.
RoleManager roleManager = Telerik.Sitefinity.Security.RoleManager.GetManager();
Role role = roleManager.GetRole("Member");
returns a null for the role object.
Is anyone able to tell me what I might be doing wrong here? Any help much appreciated.
Hi,
Run this code in debug and check what roles are retreived from the Roles provider in getAllRoles
check the list of roles and if "Member" is in it add it to the user.
//get the manager for AppRoles provider which is the default provider for roles
RoleManager roleManager = RoleManager.GetManager(
"AppRoles"
);
//check in debug how many roles can be reteived from the provider
var getAllRoles = roleManager.GetRoles();
//if Member is in the list of all roles add it to the user
var role = roleManager.GetRole(
"Member"
);
roleManager.AddUserToRole(user, role);
roleManager.SaveChanges();
Hi Stanislav
Thank you for your help, that works as I would expect now.
Possibly I'm looking in the wrong place in the documentation. I've seen no mention of the AppRoles manager until I google it specifically.
Hi,
"AppRoles" is the built in Roles provider in sitefintiy the documentation for this can be found here.
Kind regards,
Stanislav Velikov
the Telerik team