Adding user to BackendUsers role

Posted by Community Admin on 04-Aug-2018 14:33

Adding user to BackendUsers role

All Replies

Posted by Community Admin on 04-Sep-2012 00:00

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

Posted by Community Admin on 04-Sep-2012 00:00

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

Posted by Community Admin on 08-Jan-2013 00:00

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.

Posted by Community Admin on 11-Jan-2013 00:00

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

Posted by Community Admin on 11-Jan-2013 00:00

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

This thread is closed