+1-888-365-2779
Try Now
More in this section

Forums / Migrating from 3.x to the latest Sitefinity version / Issue retrieving user roles via API on an upgraded site

Issue retrieving user roles via API on an upgraded site

4 posts, 1 answered
  1. Richard
    Richard avatar
    11 posts
    Registered:
    14 Sep 2011
    05 Feb 2013
    Link to this post
    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.

     

     

  2. Stanislav Velikov
    Stanislav Velikov avatar
    1113 posts
    Registered:
    22 Sep 2016
    07 Feb 2013
    Link to this post
    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();

    Regards,
    Stanislav Velikov
    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
    Answered
  3. Richard
    Richard avatar
    11 posts
    Registered:
    14 Sep 2011
    07 Feb 2013 in reply to Stanislav Velikov
    Link to this post

    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.

  4. Stanislav Velikov
    Stanislav Velikov avatar
    1113 posts
    Registered:
    22 Sep 2016
    12 Feb 2013
    Link to this post
    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
    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
4 posts, 1 answered