Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP 3 Classic ASP Active Server Pages 3.0 > Classic ASP Professional
Classic ASP Professional For advanced coder questions in ASP 3. NOT for ASP.NET 1.0, 1.1, or 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Classic ASP Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old January 2nd, 2013, 03:05 AM
Registered User
Join Date: Dec 2012
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default LDAP AD full name and email address based on Logged in user

I am working on the a project where I should be able to get the full name and email address of Logged in User.
And after lot of modification below code works when I give User id and Password.How ever I should be able to get the first name and email address without passing Password.
Do you have any idea how this can be resolved ?I know I need to change something in the connection string.But not sure how I can achieve this.
Can you help me on this ?

'Get AD User full name and Email address
Dim oConn
Sub connActiveDir(sLogonDomain, sLogon, sPassword)
Set oConn = CreateObject("ADODB.Connection")
Set oRS = CreateObject("ADODB.Recordset")
oConn.Open ("Data Source=Active Directory Provider;Provider=ADsDSOObject;" & _
"User ID=" & sLogonDomain & "\" & sLogon & ";password=" & sPassword & ";")
End Sub

Function getDomain()
Set objRootDSE = GetObject("LDAP://RootDSE")
sConfig = objRootDSE.Get("configurationNamingContext")
getDomain = objRootDSE.Get("defaultNamingContext")
End Function

Function getLogon()
getLogon = (Mid(Request.ServerVariables("LOGON_USER"), _
InStrRev(Request.ServerVariables("LOGON_USER"), "\") + 1))
End Function

Function GetUserDetails(sDomain, sLogon)
strNAME="SELECT displayName " & _
"FROM 'LDAP://" & sDomain & "' " & _
"WHERE samaccountname='" & sLogon & "'"
'Set oRS = oConn.Execute("SELECT displayName " & _
' "FROM 'LDAP://" & sDomain & "' " & _
' "WHERE samaccountname='" & sLogon & "'")

'If Not oRs.EOF Then
'GetUserDetails = oRs("displayName")
'GetUserDetails = "No Department Listed"
'End If
End Function

connActiveDir "AD", "SXMALLA;", "Qwest#@!"

' Call a function to simply display the department of a user that is Currently logged on
Response.Write "Your fullname - " & GetUserDetails(getDomain, getLogon)

Old January 4th, 2013, 07:28 AM
Friend of Wrox
Join Date: Sep 2005
Posts: 166
Thanks: 2
Thanked 33 Times in 33 Posts

According to MSDN, if you do not pass username/password at all, you will connect as the current windows user, in this case the user the website is running as.

If you can give this user permission to search AD, which if it is an AD user already I believe it can, then you should be good to go.

If this isn't possible, and you cannot use impersonation, then you will have to pass the username and password, as you always need to "log in" to AD.
Old January 4th, 2013, 07:40 AM
Registered User
Join Date: Dec 2012
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default [Pro_ASP_How] LDAP AD full name and email address based on Logged in user

hi philip,

Now I am able to get the AD Full name and e-mail address with following code

Dim objSysInfo, objUser
Set objSysInfo =server.CreateObject("ADSystemInfo")

' Currently logged in User
Set objUser = GetObject("LDAP://" & objSysInfo.UserName)
' or specific user:
'Set objUser = GetObject("LDAP://CN=johndoe,OU=Users,DC=ss64,DC=com")
response.Write(objUser.FirstName & " ")

However this works in my localhost,it is erroring when I try with my IP Address or if I run in any other machine.It gives me the following error.

Error Type:
Microsoft VBScript runtime (0x800A0046)
Permission denied
/CIA_PTT/Preview.asp, line 7

I have even tried with multiple other ways ,all works in localhost(http://localhost/ADData.asp) but not with my IP ((
Old January 4th, 2013, 07:58 AM
Friend of Wrox
Join Date: Sep 2005
Posts: 166
Thanks: 2
Thanked 33 Times in 33 Posts

This is probably because it is being treated as an Internet/Untrusted site by your browser so it will not pass your windows NTLM credentials to it and so the site is running as the default anonymous user.

There are a few other suggestions at http://forums.iis.net/t/1153577.aspx/1 as to what else may be happening.

Similar Threads
Thread Thread Starter Forum Replies Last Post
Displaying data based on user logged in Spider Visual Web Developer 2008 23 May 19th, 2010 01:44 PM
ADO Connection problem when not logged in as an Ad Aaron Edwards Access VBA 1 January 3rd, 2008 08:43 AM
Add existing AD user to AD group via ADSI? thiazi Classic ASP Basics 0 August 24th, 2007 09:27 PM
Simple LDAP Query to AD (without password) jwadew .NET Framework 2.0 1 March 1st, 2007 12:22 PM
Filter Based Upon Logged In User LandOfToz BOOK: Professional SQL Server Reporting Services ISBN: 0-7645-6878-7 0 August 31st, 2004 12:24 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.