Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ASP get Distinquished Name from Active Directory

Status
Not open for further replies.

JennL

Programmer
Sep 11, 2003
31
US
I am using the following script to see if a user that logged into my webserver has a domain account (I'll substitute "username" for the person that is actually logged in eventually). What I really need to do is find out which container the user is in and set some variables based on that (corporate vs division access). Can anyone add to this code so the Distinguished Name prints out - I can take it from there with using inStr. This will be an include page on an IIS6 webserver.
Thanks!
Jenn

Code:
strUserName = "username"
dtStart = TimeValue(Now())
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Open "Provider=ADsDSOObject;"
 
Set objCommand = CreateObject("ADODB.Command")
objCommand.ActiveConnection = objConnection
 
objCommand.CommandText = _
    "<LDAP://dc=dorner,dc=com>;(&(objectCategory=User)" & _
         "(samAccountName=" & strUserName & 
"));distinguishedName;sAMAccountName,name;subtree"
response.Write(objCommand.CommandText)
  
Set objRecordSet = objCommand.Execute
 
If objRecordset.RecordCount = 0 Then
    response.Write("sAMAccountName: " & strUserName & " does not exist.")
Else
    response.Write(strUserName & " exists.")
End If
 
Have you tried to play with the Fields collection ?
For i = 0 to objRecordSet.Fields.Count - 1
Response.Write objRecordSet.Fields(i).Name & "=" & objRecordSet.Fields(i).Value
Next

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thanks for the suggestion PH - I think it will help to troubleshoot this further. The fields collection shows

Fields 0: distinguishedName;sAMAccountName=
Fields 1: name=cratest

Since I "borrowed" the code off the Google groups I'm not sure exactly what it does - but I think the problem is with the line
Code:
"<LDAP://dc=dorner,dc=com>;(&(objectCategory=User)(samAccountName=" & strUserName & 
"));distinguishedName;sAMAccountName,name;subtree"
Does anyone know if this is right? If it is, then how would I get the distinguishedName out of the returned recordset?

 
And this ?
"<LDAP://dc=dorner,dc=com>;(&(objectCategory=User)(samAccountName=" & strUserName &
"));distinguishedName[highlight],[/highlight]sAMAccountName,name;subtree"

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
PH You're my hero!!!
Thank you so much for the help! It works beautifully!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top