Results 1 to 3 of 3

Thread: [Resolved][2005] Authenticating A.D. User by Group

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2006
    Posts
    100

    [Resolved][2005] Authenticating A.D. User by Group

    Hi all -

    On startup of my program I would like it to verify the current user to active directory and basically determine if the user is in specific groups?

    Has anyone done this or could point me in the right direction?
    Last edited by tonydotigr; Jan 25th, 2008 at 07:44 AM.

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [2005] Authenticating A.D. User by Group

    Add a reference of System.DirectoryServices to your project then try this function
    Code:
     Public Function GetUserGroups(ByVal domain As String, ByVal usrName As String) As List(Of String)
    
            Dim groupList As New List(Of String)
            Dim adPath As String = "LDAP://CN=User,DC=" & domain & ",DC=com"
            Dim search As DirectoryServices.DirectorySearcher = Nothing
            Dim myResult As DirectoryServices.SearchResult = Nothing
            Dim myGroup As DirectoryServices.ResultPropertyValueCollection = Nothing
            Dim theGroup, strGroups() As String
    
            Try
                search = New DirectoryServices.DirectorySearcher(adPath)
                search.Filter = "(SAMAccountName=" & usrName & ")"
                search.PropertiesToLoad.Add("memberOf")
                myResult = search.FindOne
                myGroup = myResult.Properties.Item("memberOf")
    
                'Extracting group names
                groupList.Clear()
                For i As Integer = 0 To myGroup.Count - 1
                    theGroup = myGroup.Item(i).ToString().Replace("CN=", "")
                    strGroups = theGroup.Split(","c)
                    If strGroups.Length > 1 Then
                        groupList.Add(strGroups(0).Trim())
                    End If
                Next
            Catch ex As Exception
                MessageBox.Show(ex.Message)
                Return Nothing
            End Try
            Return groupList
        End Function
    You can get the domain and user name from Environment object
    Something like this
    Code:
    Dim domain As String = Environment.UserDomainName
            Dim user As String = Environment.UserName
            Dim groups As List(Of String) = GetUserGroups(domain, user)
            For Each grp As String In groups
                MsgBox(grp)
            Next
    Once you have the list of groups this user is member of, you can tell if he/she belongs to any specific group very easy.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    May 2006
    Posts
    100

    Re: [2005] Authenticating A.D. User by Group

    Thank you for the quick response, I will give this a try!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width