PC Review


Reply
Thread Tools Rate Thread

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

 
 
Chad Beckner
Guest
Posts: n/a
 
      11th Aug 2005
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


 
Reply With Quote
 
 
 
 
=?Utf-8?B?dmluYXk=?=
Guest
Posts: n/a
 
      11th Aug 2005
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=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
>
>
>

 
Reply With Quote
 
Chad Beckner
Guest
Posts: n/a
 
      11th Aug 2005
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" <(E-Mail Removed)> wrote in message
news:A788AFD8-AE82-42FC-8C68-(E-Mail Removed)...
> 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=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
>>
>>
>>



 
Reply With Quote
 
Chad Beckner
Guest
Posts: n/a
 
      11th Aug 2005
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

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





"vinay" <(E-Mail Removed)> wrote in message
news:A788AFD8-AE82-42FC-8C68-(E-Mail Removed)...
> 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=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
>>
>>
>>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Deleting non readable attribute from eDirectory - LDAP through ADSI/System.DirectoryServices chat_devil@hotmail.com Microsoft Dot NET 3 31st May 2006 02:23 AM
Deleting non readable attribute from eDirectory - LDAP through ADSI/System.DirectoryServices chat_devil@hotmail.com Microsoft Dot NET Framework 3 31st May 2006 02:23 AM
LDAP-Authencitation with System.DirectoryServices Nico Microsoft Dot NET 0 4th Jul 2005 10:41 PM
manipulating LDAP directory via System.DirectoryServices assembly bdm Microsoft C# .NET 0 8th Apr 2005 08:52 PM
LDAP problem with System.DirectoryServices goldan Microsoft ASP .NET 1 9th Mar 2005 10:42 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:11 AM.