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

Forums / Bugs & Issues / Can't get users in the role 'Administrators'

Can't get users in the role 'Administrators'

14 posts, 1 answered
  1. Kristian
    Kristian avatar
    214 posts
    Registered:
    03 Jun 2010
    20 Jan 2011
    Link to this post
    // Get the role name from the role manager.
                        IList<User> admins =
    roleManager.GetUsersInRole(
    "Administrators");

    or any of the other default roles, Editors, BackendUsers etc. Custom roles work fine.
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    20 Jan 2011
    Link to this post
    Hello Kristian,

    RoleManager.GetManager("AppRoles") - returns built-in roles. There are predefined roles that are part of applicationRoles

    var roleManager = RoleManager.GetManager("AppRoles");
    var roles = roleManager.GetRoles();

    The custom roles you created are part of the Default provider.

    var rolesForUser = roleManager.GetRolesForUser(SecurityManager.GetCurrentUserId());



    Regards,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about 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. Stacey
    Stacey avatar
    291 posts
    Registered:
    18 Oct 2012
    10 Nov 2011
    Link to this post
    I am having the same problem of not being able to retrieve a list of users for a specific role.

    I have two scenarios.
    1.) I will need to grab a list of email addresses for all users assigned to the predefined roles of Sitefinity.
    2.) I will need to grab a list of email addresses for all users assigned to custom roles.

    I have tried multiple things found in these forums and none seem to be working for me.

    var roleMngr = RoleManager.GetManager();
    var email = roleMngr.GetUsersInRole("CustomRole").FirstOrDefault().Email.ToString();

    This gives an error that value cannot be null for parameter role

    var roleMngr = RoleManager.GetManager("Default");
    var email = roleMngr.GetUsersInRole("CustomRole").FirstOrDefault().Email.ToString();

    This gives me an object reference not set to an instance of an object error because
    email is null when I try to assign it to my test label.


    var roleMngr = RoleManager.GetManager("AppRoles");
    var email = roleMngr.GetUsersInRole("Administrators").FirstOrDefault().Email.ToString();

    This gives me the same error
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    11 Nov 2011
    Link to this post
    Hello Stacey,

    Try getting the email from the user's profile

    sample code

    User user = SecurityManager.GetUser(SecurityManager.GetCurrentUserId());
    UserProfileManager userProfileManager = UserProfileManager.GetManager();
    var sitefinityProfile = userProfileManager.GetUserProfile<SitefinityProfile>(user);
    var firstName = sitefinityProfile.FirstName;
    var lastName = sitefinityProfile.LastName;
    var mail = sitefinityProfile.User.Email;



    All the best,
    Ivan Dimitrov
    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. Stacey
    Stacey avatar
    291 posts
    Registered:
    18 Oct 2012
    11 Nov 2011
    Link to this post
    That does not take care of the scenario that I need to use.  Here is more information on what I am trying to do.

    I have some custom roles that are used for department managers and department users.  I am working on a form notification that will check if the currently logged in user is a manager role or user role.  If they are a manager role an email will be sent to a predetermined email address.  If they are a department user then I am checking what department they belong to and then need to grab any/all email addresses of that departments manager users.

    With your last example I do not see how I can grab a list of users (really only need the email addresses) based on a role.  The UserProfileManager requires that you feed it a User object to do anything with it.  My problem from the start has been that I cannot seem to retrieve a list of users.


    Example:
    Logged in uer is = GenericManager
    Email will be sent to branchmanager@somedomain.com

    Logged in user is = GenericUser
    Email will be sent to the email addresses for any users assigned to GenericManager
  6. Kristian
    Kristian avatar
    214 posts
    Registered:
    03 Jun 2010
    11 Nov 2011
    Link to this post

    GetUsersInRole returns a list of users so you could use that to iterate through all users within a role and then send the email out

    var roleMngr = RoleManager.GetManager("Default");
    var users = roleMngr.GetUsersInRole("CustomRole");
     
    foreach (var user in users)
    {
      
        UserProfileManager userProfileManager = UserProfileManager.GetManager();
        var sitefinityProfile = userProfileManager.GetUserProfile<SitefinityProfile>(user);
        var firstName = sitefinityProfile.FirstName;
        var lastName = sitefinityProfile.LastName;
        var mail = sitefinityProfile.User.Email;
     
        SendMail(mail);
    }

    Or if you just want to check a role for a currently logged in user i would use

    public bool UserInCustomRole(string roleName)
            {
                bool hasRole = false;
     
                // Create a role manager using the custom roles manager.
                var roleManager = RoleManager.GetManager("Default");
                roleManager.Provider.SuppressSecurityChecks = true;
     
                // Get all the roles for the current user.
                var rolesForUser = roleManager.GetRolesForUser(SecurityManager.GetCurrentUserId());
     
                // For every role in the user compare it to the role we sent in
                // Set our boolean to true if there is a match.
                foreach (var item in rolesForUser)
                {
                    if (item.Name == roleName)
                    {
                        hasRole = true;
                    }
                }
     
                roleManager.Provider.SuppressSecurityChecks = false;
     
                // Return true / false.
                return hasRole;
            }


  7. Stacey
    Stacey avatar
    291 posts
    Registered:
    18 Oct 2012
    11 Nov 2011
    Link to this post
    Thanks for that advice Krisitan.

    I think I must have been getting the users all the time, but assumed I was not since I was trying to drill down right to the email address with this line.

    var email = roleMngr.GetUsersInRole("CustomRole").FirstOrDefault().Email.ToString();

    So my assumption from the above was that roleMngr.GetUsersInRole("CustomRole") was not even finding users

    This works for me.

    var roleMngr = RoleManager.GetManager("Default");
    var admins = roleMngr.GetUsersInRole("CustomRole");
     
    foreach(var useradmin in admins)
    {
        UserProfileManager userProfileManager = UserProfileManager.GetManager();
        var sitefinityProfile = userProfileManager.GetUserProfile<SitefinityProfile>(useradmin);
        var mail = sitefinityProfile.User.Email;
     
        TestLabel.Text += mail + ",";
    }


    I am still getting the "Object reference not set..." error when trying to pull from the predefined roles.
    var roleMngr = RoleManager.GetManager("AppRoles");
    var admins = roleMngr.GetUsersInRole("Administrators");
     
    foreach(var useradmin in admins)
    {
        UserProfileManager userProfileManager = UserProfileManager.GetManager();
        var sitefinityProfile = userProfileManager.GetUserProfile<SitefinityProfile>(useradmin);
        var mail = sitefinityProfile.User.Email;
     
        TestLabel.Text += mail + ",";
    }
  8. Kristian
    Kristian avatar
    214 posts
    Registered:
    03 Jun 2010
    11 Nov 2011
    Link to this post
    Most likely if your not logged in as an admin an hitting this code you will need to have SuppressSecurityChecks = true.
  9. Stacey
    Stacey avatar
    291 posts
    Registered:
    18 Oct 2012
    11 Nov 2011
    Link to this post
    That is a good point.  Unfortunately it does not seem to be the issue.  I literally can get the error with just these three lines of code.

    var roleMngr = RoleManager.GetManager("AppRoles");
    roleMngr.Provider.SuppressSecurityChecks = true;
    var adminUsers = roleMngr.GetUsersInRole("Administrators");
  10. Stacey
    Stacey avatar
    291 posts
    Registered:
    18 Oct 2012
    11 Nov 2011
    Link to this post
    It looks like this is definitely something to do with security.  I logged out of the test user account that I was using and logged in with my admin account.  The code to pull the admin users runs without error when logged on as an admin.  Is it possible to run a query with an impersonated user account?  I think I have seen examples somewhere about running code as a pseduo "administrator".
  11. Stacey
    Stacey avatar
    291 posts
    Registered:
    18 Oct 2012
    11 Nov 2011
    Link to this post
    As it turns out the issue seems to be that I was not getting everything cleared out of cache after each build.  Typically I do a ctl + shift in IE to force clear, but I found out that if I completely logged out and closed the browser after building and then came back in that the following will work

    var roleMngr = RoleManager.GetManager("AppRoles");
    roleMngr.Provider.SuppressSecurityChecks = true;
    var adminUsers = roleMngr.GetUsersInRole("Administrators");
  12. Conrad Ehinger
    Conrad Ehinger avatar
    59 posts
    Registered:
    22 Sep 2009
    28 Nov 2011
    Link to this post
    Just a note - 

    First name and last name are not part of the UserProfile class. Intellisense yells at me whenever I try to do it. But if I use User.FirstName and User.LastName it works (but I get a message saying its deprecated).

    I can't win here...
  13. Boyan Barnev
    Boyan Barnev avatar
    1429 posts
    Registered:
    28 Aug 2017
    01 Dec 2011
    Link to this post
    Hello Conrad Ehinger,

    I'm sorry to hear that you're having trouble accessing the FirstName and LastName information of your users. Since this information is now persisted in dynamic fields of the user's profile, you can use our extension methods GetValue() and SetValue() to get/modify this information. please note that you'll need to add a reference to Telerik.Sitefinity.Model in your class to be able to use these extension methods. I'm pasting below a code sample which demonstrates how you can properly set the FirstName and LastName upon user creation.
    public void CreateUser(string username, string password, string email, string Question, string Answer, string FirstName, string LastName)
            {
                var userManager = UserManager.GetManager("Default");
                System.Web.Security.MembershipCreateStatus status;
                userManager.CreateUser(username, password, email, Question, Answer, true, null, out status);
                userManager.SaveChanges();
                  
               var profileManager = UserProfileManager.GetManager();
               var myUser = userManager.GetUsers().Where(u => u.UserName == username).SingleOrDefault();
               var profile = profileManager.CreateProfile(myUser, typeof(SitefinityProfile).FullName);
                profile.SetValue("FirstName", FirstName);
                profile.SetValue("LastName", LastName);
      
                profileManager.SaveChanges();
      
            }
    Please do not hesitate to let us know if you need some additional information, we'll be glad to help.

    Regards,
    Boyan Barnev
    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
  14. Conrad Ehinger
    Conrad Ehinger avatar
    59 posts
    Registered:
    22 Sep 2009
    01 Dec 2011
    Link to this post
    Ahhh okay - so respectively, I need to do thisProfile.GetValue("FirstName") instead of thisProfile.FirstName

    Got it - thanks!
14 posts, 1 answered