Vb.net 当用户没有组时,在Active Directory中搜索特定组的成员失败
下面的代码应该在active directory中的所有用户中运行,并查找特定组中的每个人。一旦获得用户,它将把他们添加到一个数据表中,该数据表将是最终导出到Excel的gridview的源 然而,在运行和单步执行时,我注意到它在没有组的特定用户上被停止。我尝试添加一个条件语句来跳过该实例,但它不起作用,当发现没有组的用户时,代码仍然停止运行 有人能告诉我我可以做什么不同吗? 警告:这是我继承的一个遗留站点,对active directory知之甚少,并且是一个vb程序员新手Vb.net 当用户没有组时,在Active Directory中搜索特定组的成员失败,vb.net,active-directory,Vb.net,Active Directory,下面的代码应该在active directory中的所有用户中运行,并查找特定组中的每个人。一旦获得用户,它将把他们添加到一个数据表中,该数据表将是最终导出到Excel的gridview的源 然而,在运行和单步执行时,我注意到它在没有组的特定用户上被停止。我尝试添加一个条件语句来跳过该实例,但它不起作用,当发现没有组的用户时,代码仍然停止运行 有人能告诉我我可以做什么不同吗? 警告:这是我继承的一个遗留站点,对active directory知之甚少,并且是一个vb程序员新手 Dim entry
Dim entry As DirectoryEntry = New DirectoryEntry("LDAP://DOMAIN.EDU", "USERNAME", "PASSWORD", AuthenticationTypes.Secure)
Dim search As DirectorySearcher = New DirectorySearcher(entry) With {
.Filter = "(&(objectCategory=User)(objectClass=person))",
.PageSize = 4000
}
search.PropertiesToLoad.Add("userPrincipalName").ToString
search.PropertiesToLoad.Add("name").ToString
Dim mySearchResultColl As SearchResultCollection = search.FindAll
Dim results As DataTable = New DataTable
results.Columns.Add("User ID")
results.Columns.Add("Full Name")
Dim CurrRow = 0
For Each sr As SearchResult In mySearchResultColl
Dim dr As DataRow = results.NewRow
Dim de As DirectoryEntry = sr.GetDirectoryEntry
!!!! line below is the problem !!!!
If de.Properties("memberOf") IsNot Nothing AndAlso de.Properties("memberOf").Value.ToString = "CN=MYGROUP,OU=Security Groups,OU=Students,DC=DOMAIN,DC=edu" Then
dr("User ID") = de.Properties("userPrincipalName").Value
dr("Full Name") = de.Properties("name").Value
results.Rows.Add(dr)
de.Close
End If
Next
gvNot.DataSource = results
gvNot.DataBind()
gvNot.Visible = True
gvAgreed.Visible = False
ExportToExcel("notagreed", gvNot)
我不知道你所说的“停止”是什么意思,但你可以改变它,使其性能更好,并修复你遇到的任何问题
memberOf
属性添加一个条件来实现这一点。这样,你只会得到你关心的结果memberOf
查找成员可能无法按您希望的方式工作。我写过这方面的内容。但如果您只有一个域,则可以。)
DirectoryEntry
,因为这将迫使它返回AD并再次请求对象的属性,即使您已经在搜索结果中获得了所需的内容。因此,请改用SearchResult
对象中的值Using
子句中
PropertiesToLoad.Add上的ToString
,而不使用返回值。你可以把它去掉
Dim entry As DirectoryEntry=New DirectoryEntry(“LDAP://DOMAIN.EDU”、“用户名”、“密码”、AuthenticationTypes.Secure)
Dim search As DirectorySearcher=新的DirectorySearcher(条目),带有{
.Filter=“(&(objectCategory=User)(objectClass=person)(memberOf=CN=MYGROUP,OU=Security Groups,OU=Students,DC=DOMAIN,DC=edu))”,
.PageSize=4000
}
search.PropertiesToLoad.Add(“userPrincipalName”)
search.PropertiesToLoad.Add(“名称”)
作为DataTable的Dim结果=新DataTable
results.Columns.Add(“用户ID”)
结果.列.添加(“全名”)
Dim CurrRow=0
将mySearchResultColl用作SearchResultCollection=search.FindAll
对于每个sr作为mySearchResultColl中的搜索结果
Dim dr As DataRow=results.NewRow
dr(“用户ID”)=sr.Properties(“userPrincipalName”)(0)
dr(“全名”)=sr.Properties(“名称”)(0)
结果.行.添加(dr)
下一个
终端使用
gvNot.DataSource=结果
gvNot.DataBind()
gvNot.Visible=True
gv.Visible=False
ExportToExcel(“notagreed”,gvNot)
我看不到您在任何地方使用
CurrRow
,但我将它保留在这里,以防您在此处未显示的其他代码中使用它。尝试将String.Equals函数与StringComparer.OrdinalIgnoreCase参数一起使用,而不是使用Equals运算符。
还有,您遇到了什么错误?哦,哎呀,
CurrRow
是我以前尝试的不同代码留下的。非常感谢你的帮助,这正是我所需要的!很抱歉,在提到这个错误时不够清楚。似乎无论我做了什么,结果都是毫无争议的。我认为是过滤器工作不正常。这个运行得很好!再次感谢。:)