Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

VB.NET Active directory user member of

Posted on 2004-08-16
13
Medium Priority
?
573 Views
Last Modified: 2008-02-01
How can I get all the groups a member is in?

We will Call the member FooBar
0
Comment
Question by:Vartana
  • 7
  • 3
  • 3
13 Comments
 
LVL 3

Expert Comment

by:DJ_Back-Q
ID: 11812639
There is a "memberOf" property in the user DirectoryEntry, you can iterate over the array of objects in the property like this

You loop this array of object, convert to string and it will give you all the groups he belongs to.
0
 
LVL 1

Author Comment

by:Vartana
ID: 11812663
Example please ?
0
 
LVL 3

Expert Comment

by:DJ_Back-Q
ID: 11812680
This is what I had for example. It's in C-Sharp... pertty easy to convert in VB .NET

DirectoryEntry user = new DirectoryEntry("LDAP://server/CN=Administrator,CN=Users,DC=xxx,DC=yyyy,DC=zzzz");
PropertyCollection pcoll = user.Properties;
for (int i = 0; l < pcoll["memberOf"].Count; i++)
Console.WriteLine("\t" + (pcoll["memberOf"])[i].ToString());
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:Vartana
ID: 11812809
Can you help me convert it?
I tried but was not successful
0
 
LVL 3

Assisted Solution

by:DJ_Back-Q
DJ_Back-Q earned 400 total points
ID: 11812867
Dim user As New DirectoryEntry("LDAP://server/CN=Administrator,CN=Users,DC=xxx,DC=yyyy,DC=zzzz");

Dim pcoll As PropertyCollection = user.Properties

Dim iCounter As Integer

For iCounter = 0 To pcoll("memberOf").Count - 1

        MessageBox("Group " & pcoll("memberOf")(iCounter).ToString())

Next



Tell me if it works

Andre
MCSD
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 1600 total points
ID: 11812884
Dim user As DirectoryEntry = New DirectoryEntry ("LDAP://server/CN=Administrator,CN=Users,DC=xxx,DC=yyyy,DC=zzzz")
Dim pcoll As PropertyCollection = user.Properties

Dim i As Integer = 0
While l < pcoll("memberOf").Count
 Console.WriteLine("" + Microsoft.VisualBasic.Chr(9) + "" + (pcoll("memberOf"))(i).ToString())
 i += 1
End While

Bob
0
 
LVL 1

Author Comment

by:Vartana
ID: 11813049
no luck

Maybe this will help our domains name is

Jamba.net
0
 
LVL 1

Author Comment

by:Vartana
ID: 11813074
i think the LDAP string is wrong
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 11813077
All you really need is server name:

Dim user As New DirectoryEntry("LDAP://server/cn=Users")

Bob
0
 
LVL 1

Author Comment

by:Vartana
ID: 11813094
i did that and i get

An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in system.directoryservices.dll

Additional information: An operations error occurred
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 11813335
What is the code that you have now?

Bob
0
 
LVL 1

Author Comment

by:Vartana
ID: 11813359
   Public Sub GetADGroup()
        Dim user As DirectoryEntry = New DirectoryEntry("LDAP://Jamba.net/DC=Jamba,DC=com")
        Dim pcoll As PropertyCollection = user.Properties
        Dim i As Int32 = 0

        While i < pcoll("memberOf").Count
            Debug.Write("" + Microsoft.VisualBasic.Chr(9) + "" + (pcoll("memberOf"))(i).ToString())
            i += 1
        End While
    End Sub
0
 
LVL 1

Author Comment

by:Vartana
ID: 11815563
OK i figured this out

Final Code

    Public Function GetAdInfo(ByVal UserName As String) As String()
        Dim entry As New DirectoryEntry("LDAP://computername/CN=Users,DC=domain,DC=net")
        Dim mySearcher As New DirectorySearcher(entry)
        Dim Groups() As String

        mySearcher.Filter = "(samaccountname=" & UserName & ")"
        mySearcher.PropertiesToLoad.Add("memberof")

        Dim ResEnt As SearchResult = mySearcher.FindOne()
        If (ResEnt Is Nothing) Then
            MsgBox("You are not set-up in domain controller")
            Exit Function
        End If

        ReDim Groups(0)
        Dim prop_value As String
        For Each prop_value In ResEnt.Properties("memberof")
            'Debug.Write(Replace(Replace(prop_value, ",CN=Users,DC=pcs1,DC=net", ""), "CN=", ""))
            ReDim Preserve Groups(UBound(Groups) + 1)
            Groups(UBound(Groups)) = Replace(Replace(prop_value, ",CN=Users,DC=pcs1,DC=net", ""), "CN=", "")
        Next
        GetAdInfo = Groups
    End Function
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question