我正在使用此代码在活动目录中搜索用户帐户,效果很好。
Dim insPrincipalContext As New PrincipalContext(ContextType.Domain, Environment.UserDomainName, "DC=domain,DC=com")
Dim insUserPrincipal As New UserPrincipal(insPrincipalContext)
insUserPrincipal.Description = empID
Dim insPrincipalSearcher As New PrincipalSearcher()
Dim currentADUser As System.DirectoryServices.AccountManagement.UserPrincipal
insPrincipalSearcher.QueryFilter = insUserPrincipal
Dim results As PrincipalSearchResult(Of Principal) = insPrincipalSearcher.FindAll
For Each p As Principal In results
currentADUser = p
adUsername = currentADUser.SamAccountName
adLast = currentADUser.Surname
adFirst = currentADUser.GivenName
adMail = currentADUser.EmailAddress
Next
问题是我想要的用户帐户可能位于多个位置,因此我不想指定要搜索的 OU,但它可能位于我不希望从中返回它的 2 个 OU 中。有没有办法从我的搜索中排除这 2 个 OU?
我发现了一些类似的问题,但没有一个以这种方式使用 VB.Net 和 system.directory.accountmanagement 命名空间。