System.DirectoryServices or System.DirectoryServicesAccountManagement

kouts1
kouts1 used Ask the Experts™
on
I'm writing a web application using VB.Net 3.5.  I have everything from modifying Active Directory user properities etc... But I can't figure out how to get a list of AD groups the user belongs to with just the CN.  I need this using system. directory sevices or System.DirectoryServices.AccountManagement.  
I prefer, if possible, any ideas you guys may have be in vb.net.  
This is what I have that works, but I need to trim the result with just to show the name of the group not the whole DN. I don't need "CN=,OU=, OU=, DC=  . . ."  I just need the CN.  i know I'm missing something being a novice and all.

Dim glUserDN As String
    Dim deUserPath As String
    Dim deUserName As String
    Dim de As New DirectoryEntry("LDAP://")
    Dim iSelected As Integer

Private Sub FindUserGroup()
        Dim deSearch As New DirectorySearcher()
        deSearch.SearchRoot = de

        deSearch.Filter = "(&(objectClass=User) (employeeID=" & txtEmpID.Text.Trim() & "))"
        deSearch.PropertiesToLoad.Add("memberOf")
        deSearch.PropertiesToLoad.Add("cn")

        'Find the Result using FindAll
        Dim results As SearchResultCollection = deSearch.FindAll()
        Dim res As SearchResult
        Dim arl As New ArrayList()
        Dim i As Integer

        lstMemberOF.Items.Clear()

        If results.Count > 0 Then
            For Each res In results
                For i = 0 To res.Properties("memberOf").Count - 1
                    'Dim vVal As New ListItem()
                    'arl.Add(res.Properties("memberOf")(i).ToString())
                    lstMemberOF.Items.Add(res.Properties("memberOf")(i).ToString())
                    'vVal.Text = res.Properties("cn")(i).ToString()
                    'vVal.Value = res.Properties("memberOf")(i).ToString()
                    'lstMemberOF.Items.Add(vVal)
                Next
                glUserDN = res.Properties("AdsPath")(0).ToString()
            Next

        End If


        'deUser = results(0).GetDirectoryEntry()
        txtUSRPath.Text = results(0).Properties("AdsPath")(0).ToString()
        txtUSRName.Text = results(0).Properties("displayName")(0).ToString() 'deUser.Name


        results.Dispose()
        deSearch.Dispose()
    End Sub

Thank you!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Here is a simple functio to convert you DN's (CN=...,OU=...DC=...) into the Relativ Distinguishe Names (the  pure object names). Please note that we normally have to care about commas and other special characters in the object's name (http://www.selfadsi.org/ldap-path.htm#Comma) - but these shouldn't be existant in AD group names:

    Public Function get_rdn_pure(ByVal distinguishedName As String) As String
        'gives the friendly rdn name without "cn="
        get_rdn_pure = Split(distinguishedName, ",")(0)
        get_rdn_pure = Split(get_rdn_pure , "=")(1)
    End Function

Author

Commented:
Awesome! Thank you!!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial