Solved

LDAP Code to get Active Directory Groups

Posted on 2004-09-20
9
1,417 Views
Last Modified: 2012-05-05
Can someone please supply me with code to get the groups a member belongs to using ASP and active directory (LDAP)?

I have all sorts of code samples to try to get the active directory groups using ASP (not .Net!) but I can’t get any of them to work. They work fine until I try to get the info on a specific user.

When I try:
strAction = “LDAP://CN=fred,CN=users,DC=domain,DC=local”
Set oContainer = GetObject(strAction)

I get:
Error Type:
(0x80072030)
/GHACapEx/Common/Security.asp, line 87

Which is not very helpful at all. (Like 87 is the GetObject)

Will pay up to 500 points for brilliant solutions but any help would be greatly appreciated.

Slamhound
0
Comment
Question by:slamhound
  • 4
  • 4
9 Comments
 
LVL 11

Expert Comment

by:coopzz
Comment Utility
You most probably just entering the path wrong.

Go and find you Win 2000 support tools and look for a program called ADSI Edit.. it's brilliant for working out this stuff.

You can browse Model to what you want and show you the string that is required to access that object

heres some info to on the prog
http://www.w2kcfg.net/adsi_edit.htm

and with your example above -- it's an invalid object  it's probably going to be
LDAP://CN=fred,OU=users,DC=domain,DC=local

fred will not be the logon code it will be the name of the object in active directory.

Cheers have fun .. it's not the easist thing to get you head around but the ADSI Edit has save me confusion a million times.
0
 
LVL 6

Expert Comment

by:BubbaisBest
Comment Utility
slamhound,

I am not sure where I got this code but it will allow for me to find out what groups a user has and what users a group has.  We are using Active Directory.  The only thing you should have to change is the kearney-ne to your domain.  It also includes a sort function that I got from this list a while back.

Good luck,
Bubs

Here is my page:

<%@ Language=VBScript %>
<html>
<head>
</head>
<body>
<form action method="post" id="form1" name="form1">
<table>
<%
Dim x(500,2)

writeForm()
If Len(Request.Form("userid"))AND Request.Form("userid")<> "blank" Then
    getGroups(Request.Form("userid"))
End If
If Len(Request.Form("group")) Then
    getUsers(Request.Form("group"))
End If
Sub getGroups(userID)
    Dim strQuery, strDesc, intI
   
     strQuery = "WinNT://kearney-ne/" & userID & ",User"
    on error resume next
    Set UserObj = GetObject(strQuery)
    If userObj Then
         strDesc = userObj.Description
    Else
         strDesc = "Not Found"
    End If
    Response.Write "<tr><td><br></td></tr>"
    Response.Write "<tr class=header><td colspan=2>NT Groups for " & userID & "&nbsp-&nbsp" & strDesc & "</td></tr>"
    Response.Write "<tr class=subheader><td>Group Name</td><td>Group Description</td></tr>"
    For Each GroupObj In UserObj.Groups
         x(intI,0) = GroupObj.Name
         x(intI,1) = GroupObj.Description
         intI = intI + 1
    Next
    Call Quicksort(x,0,intI-1,0)
    Call PrintArray(x,0,intI-1)
    Set UserObj = nothing
End Sub
Sub getUsers(group)
    Dim strQuery, strDesc, intI
   
    strQuery = "WinNT://kearney-ne/" & group & ",group"
    on error resume next
    Set GroupObj = GetObject(strQuery)
    If GroupObj Then
         strDesc = GroupObj.Description
    Else
         strDesc = "Not Found"
    End If
    Response.Write "<tr><td><br></td></tr>"
    Response.Write "<tr class=header><td colspan=2>Users in NT Group " & group & "&nbsp-&nbsp" & strDesc & "</td></tr>"
    Response.Write "<tr class=subheader><td>User ID</td><td>Description</td></tr>"
    For Each UserObj In GroupObj.Members
         x(intI,0) = UserObj.Name
         x(intI,1) = UserObj.Description
         IntI = IntI + 1
    Next
    Call Quicksort(x,0,intI-1,0)
    Call PrintArray(x,0,intI-1)

    Set GroupObj = nothing
End Sub
Sub writeForm()
    Response.Write "<tr class=header><td colspan=2>Enter an YourNTDomain User ID OR Group</td></tr>"
    Response.Write "<tr class=subheader><td>User Id:</td><td><input id=userid, name=userid></input>(ie L123456)</td></tr>"
    Response.Write "<tr class=subheader><td><tc>Group:</td><td><input id=group, name=group></input>(ie USA-BUDW)</td></tr>"
    Response.Write "<tr><td><INPUT type=""submit"" value=""Submit"" id=submit1 name=submit1></td></tr>"
End Sub

Sub PrintArray(vec,lo,hi)
  '==-----------------------------------------==
  '== Print out an array from the lo bound    ==
  '==  to the hi bound.  Highlight the column ==
  '==  whose number matches parm mark         ==
  '==-----------------------------------------==

  Dim i,j
'  Response.Write "<table border=""1"" cellspacing=""0"">"
  For i = lo to hi
    If vec(i,0) <> "" then
      Response.Write "<tr>"
      For j = 0 to Ubound(vec,2)
        Response.Write "<td>" & vec(i,j) & "</td>"  
      Next
      Response.Write "</tr>"
    End If
  Next
'  Response.Write "</table>"
End Sub  'PrintArray

Sub SwapRows(ary,row1,row2)
  '== This proc swaps two rows of an array
  Dim x,tempvar
  For x = 0 to Ubound(ary,2)
    tempvar = ary(row1,x)    
    ary(row1,x) = ary(row2,x)
    ary(row2,x) = tempvar
  Next
End Sub  'SwapRows

Sub QuickSort(vec,loBound,hiBound,SortField)

  '==--------------------------------------------------------==
  '== Sort a 2 dimensional array on SortField                ==
  '==                                                        ==
  '== This procedure is adapted from the algorithm given in: ==
  '==    ~ Data Abstractions & Structures using C++ by ~     ==
  '==    ~ Mark Headington and David Riley, pg. 586    ~     ==
  '== Quicksort is the fastest array sorting routine for     ==
  '== unordered arrays.  Its big O is  n log n               ==
  '==                                                        ==
  '== Parameters:                                            ==
  '== vec       - array to be sorted                         ==
  '== SortField - The field to sort on (2nd dimension value) ==
  '== loBound and hiBound are simply the upper and lower     ==
  '==   bounds of the array's 1st dimension.  It's probably  ==
  '==   easiest to use the LBound and UBound functions to    ==
  '==   set these.                                           ==
  '==--------------------------------------------------------==

  Dim pivot(),loSwap,hiSwap,temp,counter
  Redim pivot (Ubound(vec,2))

  '== Two items to sort
  if hiBound - loBound = 1 then
    if vec(loBound,SortField) > vec(hiBound,SortField) then Call SwapRows(vec,hiBound,loBound)
  End If

  '== Three or more items to sort
 
  For counter = 0 to Ubound(vec,2)
    pivot(counter) = vec(int((loBound + hiBound) / 2),counter)
    vec(int((loBound + hiBound) / 2),counter) = vec(loBound,counter)
    vec(loBound,counter) = pivot(counter)
  Next

  loSwap = loBound + 1
  hiSwap = hiBound
 
  do
    '== Find the right loSwap
    while loSwap < hiSwap and vec(loSwap,SortField) <= pivot(SortField)
      loSwap = loSwap + 1
    wend
    '== Find the right hiSwap
    while vec(hiSwap,SortField) > pivot(SortField)
      hiSwap = hiSwap - 1
    wend
    '== Swap values if loSwap is less then hiSwap
    if loSwap < hiSwap then Call SwapRows(vec,loSwap,hiSwap)


  loop while loSwap < hiSwap
 
  For counter = 0 to Ubound(vec,2)
    vec(loBound,counter) = vec(hiSwap,counter)
    vec(hiSwap,counter) = pivot(counter)
  Next
   
  '== Recursively call function .. the beauty of Quicksort
    '== 2 or more items in first section
    if loBound < (hiSwap - 1) then Call QuickSort(vec,loBound,hiSwap-1,SortField)
    '== 2 or more items in second section
    if hiSwap + 1 < hibound then Call QuickSort(vec,hiSwap+1,hiBound,SortField)

End Sub  'QuickSort
%>
</table>
</form>
</body>
</html>
0
 
LVL 10

Author Comment

by:slamhound
Comment Utility
Good code, BubbaisBest, but it doesn't seem to require a password. I want users to be able to login (username and password) without stealing someone elses ID (username only).

Thanks, coopzz, but changing the LDAP string as you suggested results in the same error.

Any more ideas?
0
 
LVL 10

Author Comment

by:slamhound
Comment Utility
Coopzz, the ADSI Edit worked well enough to get group names for a user!!!

But what LDAP value do I have to look at to check that a username and password is correct before I check their groups?

Slamhound
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 11

Expert Comment

by:coopzz
Comment Utility
As far as I know you can't check passwords, you can only reset them, so if your trying to check there login in corectly you assign the asp page to win authenication through iis which brings up a win login box.

So if they login in successfully they will hit the asp page and you can look at the Request.ServerVariables("LOGON_USER") to grab there login code and off you go. you can get them to do this automatically if you need to. check out the thread below where I've described it before.

there trying to do the same thing.
http://www.experts-exchange.com/Web/Web_Languages/ASP/Q_21138160.html

cheers
0
 
LVL 10

Author Comment

by:slamhound
Comment Utility
One of the major issues I had earlier on is that LDAP is looking for an account name ("Selwyn Young") while I was supplying my login name ("Selwyn") and it couldn't find it. The Request.ServerVariables("LOGON_USER") provides a login name. How do I find this account using LDAP?

Slamhound
0
 
LVL 11

Accepted Solution

by:
coopzz earned 500 total points
Comment Utility
I found this with a simular thing and I modded on of the posts to fit vb.
zcolton (IS/IT--Manageme) was the name to the person.
http://www.tek-tips.com/viewthread.cfm?qid=356151

I tried this in vb6 and it worked fine so you'll have to fine tune it to your own..

Private Sub Command2_Click()
' trying to get the name of the user from sAMAccountName
    dim m_cOrganUnit as string: m_cOrganUnit = "ou=IT,ou=My Business Name"
    Dim objRoot As Object:      Set objRoot = GetObject("LDAP://RootDSE")
    Dim sDomainPath As String:  sDomainPath = objRoot.Get("DefaultNamingContext")
    Dim rs As ADODB.Recordset
    Dim con As ADODB.Connection: Set con = New ADODB.Connection
    con.provider = "ADsDSOObject"
    con.open "Active Directory Provider"
    Dim Com As ADODB.Command:  Set Com = New ADODB.Command
    Set Com.ActiveConnection = con
    Com.CommandText = "select name, sAMAccountname from 'LDAP://" & m_cOrganUnit & "," & sDomainPath & "' WHERE objectCategory='person' AND sAMAccountname='fredG' ORDER BY name"
    Set rs = Com.Execute
    MsgBox rs.Fields("name")
End Sub
0
 
LVL 10

Author Comment

by:slamhound
Comment Utility
Thanks Coopzz. We're almost there.

I can't get this code working because I have a different AD structure (I think). I get an EOF if I leave the WHERE clause in and I get "No such table" if I leave the "ou=IT" in. Using ADSIEdit I can find the ou of "MyBusiness" but can find no sAMAccountname property under any list.

Where should i look to find this value?

Slamhound
0
 
LVL 11

Expert Comment

by:coopzz
Comment Utility
sorry the sAMAccount is one of the LogonCode Propertys to get or test against (you'll have to look it up but there are 2 one for Ol' NT4 clients and then the other for the 2k above.

m_cOrganUnit  = "ou=IT,ou=My Business Name" was something specific to where I work you have to change this meet your needs.

If you don't know much about active directory you may need to talk to your administrator but bascally where I work we have a ou (Organisation Until) thats our business name and another one under it call IT or Admin or Guests, etc..  so you would have to define your paths acordingly.
 Just Use you ADSI edit to find where The users your looking for and grab the path.  I haven't messed around with recordset approach before but it appears to be so much more powerful than what I looked at before..

Cheers.

heres some of the attributes you can get.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wss/wss/_exch2k_urn_content-classes_person.asp
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now