ADS/LDAP/System.DirectoryServices -> Get Users of Group

  • Thread starter Thread starter Chad Beckner
  • Start date Start date
C

Chad Beckner

I am starting to translate some code from ASP to ASP.NET (VB). I was able
to query ADS to get a users groups that they belong to, and also query a
group and get a list of users. However, I can't seem to get this to work in
VB.Net. Here's what I have:

OLD ASP Code:

Function Get_ADS_Users_For_Group(ADS_Path)
Dim Group_Information
Dim Error_Number
On Error Resume Next
Get_ADS_Object_Counter = 0
Error_Number = 99 'Generic Error number, no actual value
Do While Error_Number <> 0 AND Get_ADS_Object_Counter < 10
If InStr(ADS_Path, "CN=") = 0 Then
Set ADS_Root = GetObject("LDAP://RootDSE")
ADS_Domain_Path = ADS_Root.Get("DefaultNamingContext")
ADS_Path = "CN=" & ADS_Path & ",OU=User
Groups,OU=Groups,OU=OU3,OU=OU4," & ADS_Domain_Path
End If
If InStr(ADS_Path, "OU=") = 0 Then
Set ADS_Root = GetObject("LDAP://RootDSE")
ADS_Domain_Path = ADS_Root.Get("DefaultNamingContext")
ADS_Path = ADS_Path & ",OU=User Groups,OU=Groups,OU=OU3,OU=OU4," &
ADS_Domain_Path
End If

'Response.Write ADS_Path
'Response.End

Set ADS_Object = GetObject("LDAP://" & ADS_Path)
Error_Number = Err.Number
If Error_Number <> 0 Then
Get_ADS_Object_Counter = Get_ADS_Object_Counter + 1
If Get_ADS_Object_Counter = 9 Then
Exit Do
End If
Pause(2)
End If
Loop
Set Group_Info = ADS_Object
On Error GoTo 0
If NOT IsEmpty(Group_Info) Then
For Each Member in Group_Info.Members
If LCase(Member.Class) = "group" Then
Group_Name = Replace(Member.Name, "CN=", "")
Group_Information = Group_Information &
Get_ADS_Users_For_Group(Group_Name)
Else
Group_Information = Group_Information & Replace(Member.Name, "CN=",
"") & ","
End If
Next
End If
Get_ADS_Users_For_Group = Group_Information
End Function


..NET Code:

Dim de As DirectoryEntry = New DirectoryEntry(LDAP://CN=SOMEGROUP,OU=User
Groups,OU=Groups,OU=OU3,OU=OU4,DC=DC1,DC=DC2,DC=DC3")
Dim child As DirectoryEntry
For Each child In de.Children
Response.Write(child.Name.ToString())
Next

Using this setup (which I've seen on numerous sites) does not display
anything...


Thanks for any help!

Chad
 
try if the basic domain name is working
Dim de As DirectoryEntry = New DirectoryEntry("LDAP://OU=Groups,DC=DC1")
Dim child As DirectoryEntry
For Each child In de.Children
Response.Write(child.Name.ToString())
Next

what i use for LDAP is example - localhost:389 OR servername is optional
"LDAP://localhost:389/DC=Yahoo,DC=com"
--
http://pathidotnet.blogspot.com
=====
vInAypAtHi
o__
---_,>/'_------
(_) \(_)
 
That would be if I want the entire tree, which I don't. I just want the
users of "that" group... Also, I have tried code like this, but it doesn't
return any results....

Chad
 
Here is what I have so far... It works, but there has got to be a better way
to do this, without using ActiveDS, etc. I would love to use the
de.Children setup...

===================================================================
Option Explicit On
Option Strict On

Imports System
Imports System.DirectoryServices
Imports System.Web.HttpContext
Imports System.Collections
Imports ActiveDs

Public Class Query_ADS
Public Shared Function GetADS_Group_Members(ByVal strGroup_Name As
String) As SortedList
Dim deGroup As DirectoryEntry
Dim MembersCollection As IADsMembers
Dim member As IADsUser
Dim objRootDSE As New DirectoryEntry("LDAP://RootDSE")
Dim slGroupMembers As New SortedList

Try
deGroup = New DirectoryServices.DirectoryEntry("LDAP://CN=" &
strGroup_Name & ",OU=User Groups,OU=Groups,OU=SOMEOU1,OU=SOMEOU2," &
objRootDSE.Properties("defaultNamingContext")(0).ToString())
MembersCollection = CType(deGroup.Invoke("Members"),
IADsMembers)
For Each member In MembersCollection
slGroupMembers.Add(member.LastName, Replace(member.Name,
"CN=", "", , , CompareMethod.Text) & "|;|" & member.ADsPath)
Next
Catch ex As Exception
Throw
Finally
If Not IsNothing(deGroup) Then deGroup.Close()
deGroup = Nothing
End Try

Return slGroupMembers
End Function
End Class

===================================================================
 
Back
Top