Solved

VB.NET Active directory user member of

Posted on 2004-08-16
13
554 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

867 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now