locked
how to get user logins of group members and not surname first name? RRS feed

  • Question

  • User1877682370 posted

    I would like to get at the user logins for all the members of a pre-selected AD group so that i can display this information on an admin page in my application (all my permissions are held in sitemap and role membership is done in AD). 

     

    I cant seem to get this working, although i can easily get the surname and firstname of all the users in the passed in group:

    Public Function getUsersInRole(ByVal roleName As String)
    
            Dim usersList As New StringBuilder()
    
            Try
                Dim search As DirectorySearcher = New DirectorySearcher()
                search.Filter = String.Format("(cn={0})", roleName.ToString())
                search.PropertiesToLoad.Add("member")
                search.PropertiesToLoad.Add("name")
    
                Dim results As SearchResultCollection = search.FindAll()
                Dim result As SearchResult
    
                If (results.Count > 0) Then
                    For Each result In results
                        For Each member As String In result.Properties("member")
                            usersList.Append(member & "/")
                        Next
                    Next
                End If
    
                Return usersList.ToString()
    
            Catch ex As Exception
                Throw ex
                Return String.Empty
            End Try
    
        End Function


     

    this returns something like:

    CN=smith John,OU=Users,OU=HQ,DC=landmarc,DC=local/CN=smith john,OU=Users,OU=HQ,DC=landmarc,DC=local/

    but as you can see there is a problem getting at any user specific info if i get 2 john smiths back in the same group. If i could get the logins (which are unique) i could get at the title, email, phone number (etc) attributes from AD.

    Can anyone help me with altering my code so I can get back the logins, or at least point me in the right direction. I do have an ADSI linked server set up on my sqlserver db so i could do this through SQL but again can't seem to find the correct syntax for this.

    If anyone knows how to achieve what i want through either method i'd be grateful to hear from them!

    cheers,

    Monday, November 2, 2009 11:40 AM

Answers

  • User1877682370 posted

    found this thread: http://forums.asp.net/t/1419464.aspx

    I now have code that looks like this:

    Dim usersList As New StringBuilder()
        
            Try
                Dim search As DirectorySearcher = New DirectorySearcher()
                search.Filter = String.Format("(cn={0})", roleName.ToString())
                search.PropertiesToLoad.Add("member")
    
                Dim results As SearchResultCollection = search.FindAll()
                Dim result As SearchResult
                Dim iCount As Integer
    
                If (results.Count > 0) Then
                    For Each result In results
                      For iCount = 0 To result.Properties("member").Count - 1
                            Dim strUser() As String = result.Properties("member")(iCount).ToString().Split(",")
                            Dim findUser As String = strUser(0).ToString()
                            search.Filter = "(" + findUser & ")"
                            search.PropertiesToLoad.Add("name")
                            search.PropertiesToLoad.Add("title")
                            Dim personResults As SearchResultCollection
                            personResults = search.FindAll()
                            Dim personResult As SearchResult
    
                            For Each personResult In personResults
                                Dim fullName As String = personResult.Properties("name")(0).ToString()
                                Dim title As String = personResult.Properties("title")(0).ToString()
                                usersList.Append(fullName & ", " & title & "/")
                            Next
                            iCount = iCount + 1
                        Next
                    Next
                End If
    
                Return usersList.ToString()
    
            Catch ex As Exception
                Throw ex
                Return String.Empty
            End Try


    Seems to be working, hope it helps anyone else who stumbles across this thread. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 3, 2009 6:10 AM