Results 1 to 5 of 5

Thread: [RESOLVED] Querying Active Directory Groups using VB2010

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2008
    Location
    Scotland
    Posts
    68

    Resolved [RESOLVED] Querying Active Directory Groups using VB2010

    Good evening

    I really hope that someone out there in the forumsphere can help me with something that's been driving me crazy all day.

    My company (a contact centre) runs a piece of web-based software which we can script so that our end users can capture information obtained from callers to our many helplines. The whole thing is back-ended onto SQL, with access to it being controlled by a single SQL 2005 database. Until now, if I want to grant users access to a particular campaign database I need to go in and manually create them a user record (if they don't have one already) and then assign them the permissions to access a particular campaign. This has been fine up to now, but the requests to add new users to the system, or grant new permissions to existing users, have become more and more frequent, as has the number of users that I'm having to create/modify.

    What I would like to be able to do is write a small VB application which could query the names of users in a pre-determined set of Security Groups on Active Directory. That information could then be passed to a series of SQL Stored Procedures which would create new users (if necessary) and assign the database access permissions. However, I'm stumped as to how to go about doing it. Years ago I had VB.NET 2002 code which could interrogate a user's Active Directory record to find out if s/he had membership of a particular group, but I'm now looking to do it the other way round.

    At this stage, I should let you know that although I have some knowledge of VB.NET, I'm by no means an expert in the field so I'd be grateful if any suggestions could be mindful of my lack of knowledge in this matter.

    Many thanks in advance.


    Ian Henderson

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

    Re: Querying Active Directory Groups using VB2010

    These 2 functions should get you what you need.

    The parameter ldapPath is the ldap path string for your company domain. For example, if your company domain is "example.net" and the domain controller is 192.168.0.254 then ldapPath = "LDAP://192.168.0.254/DC=example,DC=net".

    adminAccount is the domain userID that has domain administrator privilege, i.e. "example\Administrator". adminPwd is the password for adminAccount.


    Don't forget to add a reference to System.DirectoryServices to your project.

    vb.net Code:
    1. Imports System.DirectoryServices
    2. 'This function returns a list of all security groups in the domain directory
    3. Public Function GetSecurityGroupList(ByVal ldapPath As String, adminAccount As String, ByVal adminPwd As String) As List(Of String)
    4.         Dim grpLst As New List(Of String)
    5.         Using rootEntry As New DirectoryEntry(ldapPath, adminAccount, adminPwd)
    6.             Using searcher As New DirectorySearcher(_rootEntry)
    7.                 searcher.Filter = "(&(ObjectClass=Group))"
    8.                 Using results As SearchResultCollection = searcher.FindAll
    9.                     For Each grp As SearchResult In results
    10.                         grpLst.Add(grp.GetDirectoryEntry.Name.Substring(3))
    11.                     Next
    12.                     grpLst.Sort()
    13.                 End Using
    14.             End Using
    15.         End Using
    16.         Return grpLst
    17.     End Function
    18.  
    19. 'This function returns a datatable containing all users that are members of a given security group.
    20.     Public Function GetGroupMembers(ByVal groupName As String, ByVal ldapPath As String, adminAccount As String, ByVal adminPwd As String) As DataTable
    21.         Dim dt As New DataTable(groupName)
    22.         With dt.Columns
    23.             .Add("AccountID", GetType(String))
    24.             .Add("FirstName", GetType(String))
    25.             .Add("LastName", GetType(String))
    26.             .Add("DisplayName", GetType(String))
    27.             .Add("AccountDisabled", GetType(Boolean))
    28.         End With
    29.  
    30.         Using rootEntry As New DirectoryEntry(ldapPath, adminAccount, adminPwd)
    31.             Using searcher As New DirectorySearcher(rootEntry)
    32.                 If groupName.ToUpper = "DOMAIN USERS" Then
    33.                     searcher.Filter = "(&(objectCategory=person)(objectClass=user))"
    34.                     Using results As SearchResultCollection = searcher.FindAll
    35.                         Dim user As DirectoryEntry = Nothing
    36.                         For Each result As SearchResult In results
    37.                             user = result.GetDirectoryEntry
    38.                             Dim props As PropertyCollection = user.Properties
    39.                             dt.Rows.Add(props("sAMAccountName").Value, props("givenName").Value, props("sn").Value, props("displayName").Value, CBool(user.InvokeGet("AccountDisabled")))
    40.                         Next
    41.                     End Using
    42.                 Else
    43.                     searcher.Filter = String.Format("(&(ObjectClass=Group)(CN={0}))", groupName)
    44.                     Dim result As SearchResult = searcher.FindOne
    45.                     Dim members As Object = result.GetDirectoryEntry.Invoke("Members", Nothing) '<<< Get members
    46.                     For Each member As Object In CType(members, IEnumerable)  '<<< loop through members
    47.                         Dim currentMember As New DirectoryEntry(member) '<<< Get directoryentry for user
    48.                         If currentMember.SchemaClassName.ToLower = "user" Then
    49.                             Dim props As PropertyCollection = currentMember.Properties
    50.                             dt.Rows.Add(props("sAMAccountName").Value, props("givenName").Value, props("sn").Value, props("displayName").Value, CBool(currentMember.InvokeGet("AccountDisabled")))
    51.                         End If
    52.                     Next
    53.                 End If
    54.             End Using
    55.         End Using
    56.         Return dt
    57.     End Function
    Last edited by stanav; Feb 21st, 2013 at 03:27 PM.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Sep 2008
    Location
    Scotland
    Posts
    68

    Re: Querying Active Directory Groups using VB2010

    Stanav

    You're a legend. I've now taken the second function (GetGroupMembers) and have been able to scan through a specified group and extract the appropriate information using it. My next trick is to write the code that will then inject that information into my SQL database, creating the user if necessary and updating where needed.

    Many thanks

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Sep 2008
    Location
    Scotland
    Posts
    68

    Re: Querying Active Directory Groups using VB2010

    On the back of the information provided by stanav, can anyone tell me where I can find a full list of the properties that could be retrieved by the code that's been provided?

    Thanks

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

    Re: Querying Active Directory Groups using VB2010

    When searching for these kind of information, MSDN is usually my first place to go to. Of course I do use Google Try this:
    http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

Tags for this Thread

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