I have a lovely little function that collects a user's email address from AD. It works just the way I want to in VBScript & VB & VBA.
I would like to use it in a web application I'm building, so I've modified it for ASP:
I'm sure you'll agree, nothing major's changed, but when I run the page I just get
IIS is set up to use Integrated Windows Authentication. If I change it so that it uses Basic Authentication, it works, but this means users have to log in to the system again, which is not ideal.
IIS is not running on the DC.
I've tried to work out a solution from Googling round, but I'm at an impasse. Please can someone help me. I'm starting to lose the will to live.
Cheers
Ben
----------------------------------------------
Ben O'Hara "Where are all the stupid people from...
...And how'd they get so dumb?"
NoFX-The Decline
----------------------------------------------
Want to get great answers to your Tek-Tips questions? Have a look at F
Code:
Public Function Email(LoginName As String) As Variant
'PURPOSE: Display Email Address that is available in
'the Active Directory about a given user
'PARAMETER: Login Name for user
'RETURNS: String with selected information about
'user, or empty string if there is no such
'login on the current domain
'REQUIRES: Windows 2000 ADSI, LDAP Provider
'Proper Security Credentials.
'EXAMPLE: msgbox EMail("Administrator")
Static conn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim oRoot As IADs
Dim oDomain As IADs
Dim oGroup As IADsGroup
Dim sBase As String
Dim sFilter As String
Dim sDomain As String
Dim sAttribs As String
Dim sDepth As String
Dim sQuery As String
Dim sAns As Variant
Dim user As IADsUser
On Error GoTo ErrHandler:
sAns = Null
If conn Is Nothing Then
Set conn = New ADODB.Connection
End If
'Get user Using LDAP/ADO. There is an easier way
'to bind to a user object using the WinNT provider,
'but this way is a better for educational purposes
Set oRoot = GetObject("LDAP://rootDSE")
'work in the default domain
sDomain = oRoot.Get("defaultNamingContext")
Set oDomain = GetObject("LDAP://" & sDomain)
sBase = "<" & oDomain.ADsPath & ">"
'Only get user name requested
sFilter = "(&(objectCategory=person)(objectClass=user)(name=" _
& LoginName & "))"
sAttribs = "adsPath"
sDepth = "subTree"
sQuery = sBase & ";" & sFilter & ";" & sAttribs & ";" & sDepth
If conn.State = adStateClosed Then
conn.Open _
"Data Source=Active Directory Provider;Provider=ADsDSOObject"
End If
Set rs = conn.Execute(sQuery)
If Not rs.EOF Then
Set user = GetObject(rs("adsPath"))
With user
'if the attribute is not stored in AD,
'an error will occur. Therefore, this
'will return data only from populated attributes
On Error Resume Next
sAns = .EmailAddress
On Error GoTo ErrHandler:
End With
End If
Email = sAns
ErrHandler:
On Error Resume Next
If Not rs Is Nothing Then
If rs.State <> 0 Then rs.Close
Set rs = Nothing
End If
Set oRoot = Nothing
Set oDomain = Nothing
End Function
I would like to use it in a web application I'm building, so I've modified it for ASP:
Code:
<%
Response.Write Email("721116")
Public Function Email(LoginName)
'PURPOSE: Display Email Address that is available in
'the Active Directory about a given user
'PARAMETER: Login Name for user
'RETURNS: String with selected information about
'user, or empty string if there is no such
'login on the current domain
'REQUIRES: Windows 2000 ADSI, LDAP Provider
'Proper Security Credentials.
Dim conn
Dim rs
Dim oRoot
Dim oDomain
Dim oGroup
Dim sBase
Dim sFilter
Dim sDomain
Dim sAttribs
Dim sDepth
Dim sQuery
Dim sAns
Dim user
sAns = Null
Set conn = Server.CreateObject("ADODB.Connection")
Set oRoot = GetObject("LDAP://rootDSE")
sDomain = oRoot.Get("defaultNamingContext")
[COLOR=red]Set oDomain = GetObject("LDAP://" & sDomain)[/color]
sBase = "<" & oDomain.ADsPath & ">"
sFilter = "(&(objectCategory=person)(objectClass=user)(name=" _
& LoginName & "))"
sAttribs = "adsPath"
sDepth = "subTree"
sQuery = sBase & ";" & sFilter & ";" & sAttribs & ";" & sDepth
conn.Open _
"Data Source=Active Directory Provider;Provider=ADsDSOObject"
Set rs = conn.Execute(sQuery)
If Not rs.EOF Then
Set user = GetObject(rs("adsPath"))
With user
On Error Resume Next
sAns = .EmailAddress
On Error GoTo 0
End With
End If
Email = sAns
On Error Resume Next
If rs.State <> 0 Then rs.Close
Set rs = Nothing
Set oRoot = Nothing
Set oDomain = Nothing
End Function
%>
I'm sure you'll agree, nothing major's changed, but when I run the page I just get
Code:
error '80072020'
/phonebook/Default.asp, line 39 (Highlighted in red)
IIS is set up to use Integrated Windows Authentication. If I change it so that it uses Basic Authentication, it works, but this means users have to log in to the system again, which is not ideal.
IIS is not running on the DC.
I've tried to work out a solution from Googling round, but I'm at an impasse. Please can someone help me. I'm starting to lose the will to live.
Cheers
Ben
----------------------------------------------
Ben O'Hara "Where are all the stupid people from...
...And how'd they get so dumb?"

----------------------------------------------
Want to get great answers to your Tek-Tips questions? Have a look at F