Language: VB6 (possibly converted to VBScript/ASP for webapp)
Requirements: Active Directory
VB6 References: Active DS Library, Microsoft ActiveX Data Objects

I'm intending to convert this code into VBScript for use in a ASP Webapp. The function accepts a username (eg. s009999) and a groupname (eg. ITBranch), and uses an LDAP Query to verify whether the username is a member of groupname in Active Directory.
VB Code:
  1. Public Function FindUserGroupInfo(LoginName As String, GroupName As String) As Boolean
  2. ' Searches for a user within a specified group in Active Directory.
  3. ' Returns TRUE if the user is found in the specified group.
  4. ' Returns FALSE if the user is not found in the group.
  5.  
  6.     ' LDAP Search Query Properties
  7.     Dim conn As New ADODB.Connection    ' ADO Connection
  8.     Dim rs As ADODB.Recordset           ' ADO Recordset
  9.     Dim oRoot As IADs
  10.     Dim oDomain As IADs
  11.     Dim sBase As String
  12.     Dim sFilter As String
  13.     Dim sDomain As String
  14.     Dim sAttribs As String
  15.     Dim sDepth As String
  16.     Dim sQuery As String
  17.     Dim sAns As String
  18.    
  19.     ' Search Results
  20.     Dim user As IADsUser
  21.     Dim group As Variant
  22.     Dim usergroup As String
  23.     Dim userGroupFound As Boolean
  24.    
  25.     On Error GoTo ErrHandler:
  26.    
  27.     userGroupFound = False
  28.    
  29.     'Set root to LDAP/ADO.
  30.     Set oRoot = GetObject("LDAP://rootDSE")
  31.    
  32.     'Create the Default Domain for the LDAP Search Query
  33.     sDomain = oRoot.Get("defaultNamingContext")
  34.     Set oDomain = GetObject("LDAP://" & sDomain)
  35.     sBase = "<" & oDomain.ADsPath & ">"
  36.    
  37.     ' Set the LDAP Search Query properties
  38.     sFilter = "(&(objectCategory=person)(objectClass=user)(name=" & LoginName & "))"
  39.     sAttribs = "adsPath"
  40.     sDepth = "subTree"
  41.     sQuery = sBase & ";" & sFilter & ";" & sAttribs & ";" & sDepth
  42.    
  43.     ' Open the ADO connection and execute the LDAP Search query
  44.     conn.Open "Data Source=Active Directory Provider;Provider=ADsDSOObject"
  45.     Set rs = conn.Execute(sQuery)   ' Store the query results in recordset
  46.    
  47.     ' Display the user details
  48.     If Not rs.EOF Then
  49.         Set user = GetObject(rs("adsPath"))
  50.  
  51.         ' Display the groups memberships
  52.         For Each group In user.Groups
  53.             usergroup = group.name
  54.            
  55.             If (InStr(usergroup, GroupName) > 0) Then
  56.                 FindUserGroupInfo = True
  57.                 Exit Function
  58.             End If
  59.         Next
  60.     End If
  61.     FindUserGroupInfo = userGroupFound
  62. ErrHandler:
  63.    
  64.     On Error Resume Next
  65.     If Not rs Is Nothing Then
  66.         If rs.State <> 0 Then rs.Close
  67.         Set rs = Nothing
  68.     End If
  69.    
  70.     If Not conn Is Nothing Then
  71.         If conn.State <> 0 Then conn.Close
  72.         Set conn = Nothing
  73.     End If
  74.    
  75.     Set oRoot = Nothing
  76.     Set oDomain = Nothing
  77. End Function
Unfortunately our team is far too busy to peer-review code (at least that's their excuse). So I'd value people's criticisms and suggestions on how to improve this piece of code.

Thanks!