More in this section

Forums / Developing with Sitefinity / Adding user to BackendUsers role

Adding user to BackendUsers role

5 posts, 0 answered
  1. Richard
    Richard avatar
    15 posts
    Registered:
    17 Aug 2012
    04 Sep 2012
    Link to this post
    Hello,

    I am trying to programmatically add a user to the BackendUsers role (which occurs via the site, when you check the 'can access backend' box when creating a user). 

    I do know you can call:

    user.IsBackendUser = true;

    This sets the user as being Is_Backend_User (sets this to 1) in the database. However, unlike what is said in the documentation, it does not seem to have the same function as checking the box in the backend.

    To function, my code requires that the user be in the BackendUsers role explicitly. I have tried manually adding this role to the user:

    if (userManager.UserExists(user.UserName))
                {
                    foreach (string roleName in rolesToAdd)
                    {
                        if (roleManager.RoleExists(roleName))
                        {
                            Role role = roleManager.GetRole("BackendUsers");
                            roleManager.AddUserToRole(user, role);
                        }
                    }
                }

    However, the role is returned as null (I guess because it is not a custom role). 

    Can anybody suggest how I can manually add the BackendUsers role to the specified user?

    Thanks in advance,
    Richard
  2. Richard
    Richard avatar
    15 posts
    Registered:
    17 Aug 2012
    04 Sep 2012
    Link to this post
    I have solved this. For reference:

    Somebody recommended I use Chrome tools to see what the Sitefinity backend was doing. This proved very useful and I found that in order to access the BackendUsers role, you have to use the AppRoles role provider.

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

    This now works.

    Hope this helps others.

    Thanks,
    Richard
  3. Arno
    Arno avatar
    249 posts
    Registered:
    08 Sep 2010
    08 Jan 2013 in reply to Richard
    Link to this post
    Thanks Richard, the RoleManager did not work for me until I specifically requested that provider.

    Does anyone know why RoleManager.GetManager() does not work by default? I don't think I've done anything special with the security providers. I'm using Sitefinity 5.3 and claims authentication.
  4. Jen Peleva
    Jen Peleva avatar
    568 posts
    Registered:
    10 Nov 2016
    11 Jan 2013
    Link to this post
    Hi Arno,

     The problem comes from the fact that some roles that you query are in different provider than the "Default" (only the default roles provider is requested with GetProvider(). In order to get all roles for a specific user you need to check all providers for the user's roles. Here's an example code:

    var providers = RoleManager.GetManager().Providers;
               foreach (var provider in providers)
               {
                   var manager = RoleManager.GetManager(provider.Name).Provider;
                   manager.SuppressSecurityChecks = true;
                   string roleName = "Custom";
                   Role MyRole = manager.GetRoles().Where(r => r.Name == roleName).FirstOrDefault();
                   var uManager = UserManager.GetManager();
                 var loggedInUser = uManager.GetUser("Approver1");
      
                   if (MyRole != null)
                   {
      
                       var roleid = MyRole.Id;
                       if (manager.RoleExists(MyRole.Name))
                       {
                           //var users = manager.GetUsersInRole(roleid).ToList();
                           manager.IsUserInRole(loggedInUser.Id, roleid);
                       }
                   }
               }
           }


    Regards,
    Jen Peleva
    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
  5. Arno
    Arno avatar
    249 posts
    Registered:
    08 Sep 2010
    11 Jan 2013 in reply to Jen Peleva
    Link to this post

    Thanks Jen, I had no idea about that. Looping through all providers makes my code work in all cases.

5 posts, 0 answered