2012-04-04 44 views
2
搜索整个域的用户在AD

我知道如何使用精确LDAP URL如何使用VB.NET

LDAP://domain/CN=Username,OU=Users,DC=domain,DC=com 

但如何找到用户,如果我需要找到一个用户,而不看在特定的OU。我如何搜索整个域?

回答

0

只需从LDAP路径中省略OU结构即可。这会将搜索设置在域的根目录。

LDAP://DC=domain,DC=com 

,并使用过滤器来查找特定用户:

(&(objectClass=User)(cn=" & susername & ")) 
+0

注意,搜索范围应该是'在这个例子中sub'。 – 2012-04-04 19:26:25

+0

所以使用Tom Pickles的建议,我想出了这个 – Pickle 2012-04-05 00:01:21

3

如果您使用.NET 3.5或更新版本,你应该看看PrincipalSearcher类:

' create your domain context 
Dim ctx As New PrincipalContext(ContextType.Domain) 

' define a "query-by-example" principal - here, we search for a UserPrincipal 
' and with the first name (GivenName) of "Bruce" and a last name (Surname) of "Miller" 
Dim qbeUser As New UserPrincipal(ctx) 
qbeUser.GivenName = "Bruce" 
qbeUser.Surname = "Miller" 

' create your principal searcher passing in the QBE principal  
Dim srch As New PrincipalSearcher(qbeUser) 

' find all matches 
For Each found As var In srch.FindAll() 
    ' do whatever here - "found" is of type "Principal" - it could be user, group, computer.....   
Next 

如果您尚未阅读MSDN文章Managing Directory Security Principals in the .NET Framework 3.5,该文章很好地展示了如何充分利用System.DirectoryServices.AccountManagement中的新功能。或者查看MSDN documentation on the System.DirectoryServices.AccountManagement命名空间。

当然,这取决于你的需要,你可能想在你创建一个“查询通过例如”用户主体指定其他属性:

  • DisplayName(通常为:第一名称+空格+姓氏)
  • SAM Account Name - 你的Windows/AD帐户名
  • User Principal Name - 你的 “[email protected]” 样式名

可以SPE将UserPrincipal上的任何属性都作为属性,并将它们用作您的PrincipalSearcher的“查询范例”。

或者,如果你想找到只特定用户 - 试试这个:

' find a user 
Dim user As UserPrincipal = UserPrincipal.FindByIdentity(ctx, "SomeUserName") 

' do something here....  
If user IsNot Nothing Then 
    . ..... 
End If