2012-04-04 65 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