2013-12-13 158 views
-1

有谁知道在Active Directory中使用DirectoryServices搜索单个用户的最佳方法吗?我有代码,目前列出给定的LDAP路径下的所有子'OU',但我现在想要添加搜索路径下的用户的功能。代码是否适合搜索用户?ASPX C#在Active Directory中搜索用户

我已经包含了我的代码,列出了所有用户在当前的OU:

DirectoryEntry Ldap = new DirectoryEntry("LDAP://" + ouselect.SelectedValue + ";" + LDAPRoot, LDAPUser, LDAPPass); 
DirectorySearcher ad_search = new DirectorySearcher(Ldap); 

ad_search.Filter = "(objectClass=User)"; 
ad_search.SearchScope = SearchScope.Subtree; 
ad_search.PropertiesToLoad.Add("samaccountname"); 

任何指针任何人都可以提供将是极好的。

回答

2

如果您使用.NET 3.5及更高版本,则应检查System.DirectoryServices.AccountManagement(S.DS.AM)命名空间。在这里阅读全部内容:

基本上,你可以定义域范围内,并可以轻松地查找用户和/或组AD:

// set up domain context 
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain)) 
{ 
    // find a user 
    UserPrincipal user = UserPrincipal.FindByIdentity(ctx, "SomeUserName"); 

    if(user != null) 
    { 
     // do something here....  
    } 
} 

的新的S.DS.AM可以很容易地与AD中的用户和群组玩耍!

PS:PrincipalContext对其构造函数有许多不同的重载 - 您还可以定义用于查询Active Directory的用户名/密码,并且还可以定义一个“起始”容器(如果需要)。 Check out the MSDN documentation for details on this

0

你的代码几乎在那里。只需更改过滤器即可搜索特定的AD属性,而不是所有用户。

ad_search.Filter = string.Format("(department={0})", department);

ad_search.Filter = string.Format("(displayName={0})", "James Doe");

ad_search.Filter = string.Format("(sAMAccountName={0})", "some.username");

+1

你还是会希望包括过滤器的基础对象类的组成部分。 –

相关问题