Can't get users in the role 'Administrators'
// Get the role name from the role manager.
IList<User> admins =
roleManager.GetUsersInRole(
"Administrators"
);
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
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
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;
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
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;
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();
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 +
","
;
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 +
","
;
Most likely if your not logged in as an admin an hitting this code you will need to have SuppressSecurityChecks = true.
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"
);
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".
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"
);
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...
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();
Ahhh okay - so respectively, I need to do thisProfile.GetValue("FirstName") instead of thisProfile.FirstName
Got it - thanks!