Hi everyone,
I've made an .asp webpage where a user inputs a user name and the code returns the groups that that user is a member of.
The only problem is, the code only works when I enter a group name (i.e. it will return groups that a group is member of), but not a user name.
The code is below. It starts searching at the root of my DC and searches for every object type, so I'm fairly certain that isn't the problem.
Any help would be GREATLY appreciated. Thanks!
<%@ Language=VBScript %> <% Option Explicit %>
<%
Sub SingleSorter(byref arrArray)
Dim row, j
Dim StartingKeyValue, NewKeyValue, swap_pos
For row = 0 To UBound( arrArray ) - 1
'Take a snapshot of the first element
'in the array because if there is a
'smaller value elsewhere in the array
'we'll need to do a swap.
StartingKeyValue = arrArray ( row )
NewKeyValue = arrArray ( row )
swap_pos = row
For j = row + 1 to UBound( arrArray )
'Start inner loop.
If arrArray ( j ) < NewKeyValue Then
'This is now the lowest number -
'remember it's position.
swap_pos = j
NewKeyValue = arrArray ( j )
End If
Next
If swap_pos <> row Then
'If we get here then we are about to do a swap
'within the array.
arrArray ( swap_pos ) = StartingKeyValue
arrArray ( row ) = NewKeyValue
End If
Next
End Sub
%>
<html>
<head>
<meta http-equiv="Content-Language" content="en-us">
<meta name="GENERATOR" content="Microsoft FrontPage 6.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Active Directory Query - Group Members</title>
</head>
<body>
<p><font face="Verdana" size="2">
<%
' ********************************************************************************
' This script queries the Active Directory server using specified parameters
' Anonymous calls are not allowed & user authentication values must be passed.
' LDAP URL format is used to construct query.
Dim strUserName
if (request("user") <> "") Then
strUserName = request("user")
Dim strMail, strMember, strUser, strCN, strDesc, strPath, strFilter, strDomainNC, searchRoot, strQuery, arrstr, arrstr2, arrstr3, strGroup, ReturnValue, I
Dim oConnection
Dim oCommand
Dim oRS
Set oConnection = CreateObject("ADODB.Connection")
Set oCommand = CreateObject("ADODB.Command")
oConnection.Provider = "ADsDSOObject" 'The ADSI OLE-DB provider
'Response.Write strUser&"<BR>"
' Account in active directory used to authenticate query
oConnection.Properties("User ID") = "cn=****,ou=****,DC=example,DC=example,DC=example"
oConnection.Properties("Password") = "test"
oConnection.Properties("Encrypt Password") = True
oConnection.Open "ADs Provider"
' AD fields to return in query
strCN="cn"
strDesc="description"
strMember="memberOf"
strPath=strCN&","&strMember 'use ADsPath to view the LDAP path
' Query filter
strFilter="(cn="&strUserName&")"
'AD server
strDomainNC="server.example.com:389"
'AD search root
searchRoot="/dc=domainnamedot,dc=dot whatever,dc=dot whatever"
' Query on usersk
strQuery = "<LDAP://"&strDomainNC&searchRoot&">;(&(objectClass=*)"&strFilter&");"&strPath&";subtree"
'response.write "LDAP://"&strDomainNC&searchRoot&strQuery&"<br><br>"
' Set connection & execute query
oCommand.ActiveConnection = oConnection
oCommand.CommandText = strQuery
oCommand.Properties("Page Size") = 99
oCommand.Properties("Sort On") = "cn"
Set oRS = oCommand.Execute
'response.write oRS.RecordCount & " entries found<br>"
'Checks to see if there is a result in the query, if not it produces an error message. If there is a result, it performs the search and displays the results
If Not oRS.EOF Then
'Display results
Do While Not oRS.EOF Or oRS.BOF
Response.Write "User " & "<B>" & strUserName & "</B>" & " is a member of" & "<BR>" & "<BR>"
ReturnValue = oRS.Fields(1)
If IsArray(ReturnValue) Then
SingleSorter ReturnValue
For I = LBound(ReturnValue) To UBound(ReturnValue)
If ReturnValue(I) <> "" Then
arrstr = Split(ReturnValue(I),",")
arrstr2 = Split(arrstr(0),"=")
arrstr3 = Split(arrstr2(1),"\")
response.write((arrStr3(0)))
Response.Write "<BR>"
End If
Next
Response.Write "<BR>"
Else
Response.Write ReturnValue & "<BR>"
End If
oRS.MoveNext
Loop
Else
response.write "User not found (must match exactly, but not case-sensitive), please go back and re-enter"
End If
Else
%> </p>
<form method="GET">
<p><font face="Arial">** Domain (AD) User Name: domain name\<input type="text" name="user" size="20">
<input type="submit" value="Show Users" name="B1"></font></p>
</form>
<%
End if
%>
<p></p>
</font>
</body>
</html>