[英]How I get the users of a folder in Active Directory?
hi I want to build a LDAP Query how I get all User in a under folder... For example: 嗨,我想建立一个LDAP查询,我如何将所有用户放在一个文件夹中...例如:
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. 我想从Active Directory中将所有用户ins放在此文件夹中。 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: 如果使用的是.NET 3.5或更高版本,则可以使用PrincipalSearcher
和“按示例查询”主体进行搜索:
// 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
. 如果您还没有-一定要阅读MSDN文章.NET Framework 3.5中的 “ 管理目录安全性主体”,它很好地展示了如何充分利用System.DirectoryServices.AccountManagement
中的新功能。 Or see the MSDN documentation on the System.DirectoryServices.AccountManagement namespace. 或参阅System.DirectoryServices.AccountManagement命名空间上的MSDN文档 。
You'll need to add a reference to the System.DirectoryServices.AccountManagement
assembly in your references, and you'll need a line like this: 您需要在引用中添加对System.DirectoryServices.AccountManagement
程序集的引用,并且需要这样的一行:
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
. 您可以在UserPrincipal
上指定任何属性,并将它们用作PrincipalSearcher
“按示例查询”。
You can use System.DirectoryServices Namespace. 您可以使用System.DirectoryServices命名空间。
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*))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.