Solved

VB.NET Active directory user member of

Posted on 2004-08-16
13
561 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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 100 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 400 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

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!

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

752 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