By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,127 Members | 1,343 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,127 IT Pros & Developers. It's quick & easy.

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

P: n/a
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=DC 3")
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
Nov 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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__
---_,>/'_------
(_) \(_)
---------------
"Chad Beckner" wrote:
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=DC 3")
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

Nov 19 '05 #2

P: n/a
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

"vinay" <vi***@discussions.microsoft.com> wrote in message
news:A7**********************************@microsof t.com...
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__
---_,>/'_------
(_) \(_)
---------------
"Chad Beckner" wrote:
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=DC 3")
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

Nov 19 '05 #3

P: n/a
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).T oString())
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

================================================== =================

"vinay" <vi***@discussions.microsoft.com> wrote in message
news:A7**********************************@microsof t.com...
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__
---_,>/'_------
(_) \(_)
---------------
"Chad Beckner" wrote:
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=DC 3")
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

Nov 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.