I have read several posts, but I haven't been able to get anything to work for me. I would like to create an arraylist of all display names within the "Standard Users" OU. I will then use that arraylist to populate a dropdown list. I found the following in another thread, but it provides groups and not users within the specified OU:
public ArrayList Groups()
{
ArrayList groups = new ArrayList();
foreach (System.Security.Principal.IdentityReference group in
System.Web.HttpContext.Current.Request.LogonUserIdentity.Groups)
{
groups.Add(group.Translate(typeof
(System.Security.Principal.NTAccount)).ToString());
}
return groups;
}
Can this be modified to provide the list of users in the Standard Users OU? Or should I use a different approach?
I also found this, but I get an error (red lines under 'std_users') stating 'not all code paths return a value'.
public ArrayList std_users()
{
// List of strings for your names
List<string> allUsers = new List<string>();
// create your domain context and define the OU container to search in
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "myco.org", "OU=Standard Users, dc=myco, dc=org");
// define a "query-by-example" principal - here, we search for a UserPrincipal (user)
UserPrincipal qbeUser = new UserPrincipal(ctx);
// create your principal searcher passing in the QBE principal
PrincipalSearcher srch = new PrincipalSearcher(qbeUser);
// find all matches
foreach (var found in srch.FindAll())
{
// do whatever here - "found" is of type "Principal" - it could be user, group, computer.....
allUsers.Add(found.DisplayName);
}
}
Any help would be greatly appreciated.
I am using c#, Visual Studio 2013 and the framework is 4.5.1.
First off: STOP USING ArrayList
- that type is dead since .NET 2.0 and should not be used - use List<string>
(or more generally: List<T>
) instead - much better for performance and usability!
You can use a PrincipalSearcher
and a "query-by-example" principal to do your searching:
public List<string> GetAllEmailsFromUsersContainer()
{
List<string> users = new List<string>();
// create your domain context and bind to the standard CN=Users
// container to get all "standard" users
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain, null, "CN=Users,dc=YourCompany,dc=com"))
{
// define a "query-by-example" principal - here, we search for a UserPrincipal
// which is not locked out, and has an e-mail address
UserPrincipal qbeUser = new UserPrincipal(ctx);
qbeUser.IsAccountLockedOut = false;
qbeUser.EmailAddress = "*";
// create your principal searcher passing in the QBE principal
PrincipalSearcher srch = new PrincipalSearcher(qbeUser);
// find all matches
foreach(var found in srch.FindAll())
{
users.Add(found.EmailAddress);
}
}
return users;
}
Add return allUsers;
to std_users()
and let us know if that works for you.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.