hi I want to build a LDAP Query how I get all User in a under folder... For example:
OU=DBG,OU=THINCLIENT,OU=NPS,OU=services,DC=YourDomain,DC=com
I want to get all Users ins this folder from the Active Directory. For this I have a Query but I don't know how I get the users of this Folder :(
(&(objectClass=user)(objectCategory=user)(??????))
If you're using .NET 3.5 or newer, you can use a PrincipalSearcher
and a "query-by-example" principal to do your searching:
// create your domain context
string container = "OU=DBG,OU=THINCLIENT,OU=NPS,OU=services,DC=YourDomain,DC=com";
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "YourDomain", container))
{
// define a "query-by-example" principal - here, we search for UserPrincipal
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.....
}
}
If you haven't already - absolutely read the MSDN article Managing Directory Security Principals in the .NET Framework 3.5 which shows nicely how to make the best use of the new features in System.DirectoryServices.AccountManagement
. Or see the MSDN documentation on the System.DirectoryServices.AccountManagement namespace.
You'll need to add a reference to the System.DirectoryServices.AccountManagement
assembly in your references, and you'll need a line like this:
using System.DirectoryServices.AccountManagement;
at the top of your code-behind file for this to work.
You can specify any of the properties on the UserPrincipal
and use those as "query-by-example" for your PrincipalSearcher
.
You can use System.DirectoryServices Namespace.
DirectoryEntry scope = new DirectoryEntry("LDAP://OU=DBG,OU=THINCLIENT,OU=NPS,OU=services,DC=YourDomain,DC=com");
string filter = "(&(objectClass=user)(objectCategory=user))";
string[] attrs = new string[]{"samaccountname","whencreated"};
DirectorySearcher searcher = new DirectorySearcher(scope,filter,attrs);
foreach(SearchResult result in searcher.FindAll())
{
//result.Properties["attribute"][0].ToString();
}
尝试
(&(objectClass=user)(objectCategory=user)(homeDirectory=*YourFolderName*))
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.