Retrieving users email address

Discussion in 'ASP General' started by CJM, Nov 19, 2004.

  1. CJM

    CJM Guest

    I'm trying to retrieve the users email address via ADSI in ASP.

    So far, I have the following code:

    Set oUser = GetObject("WinNT://domain/" & Request.Form("UID") & ",user")
    Response.Write oUser.EmailAddress

    But I get the following message:

    "The directory property cannot be found in the cache. "

    Any ideas where I am going wrong?

    Thanks
     
    CJM, Nov 19, 2004
    #1
    1. Advertising

  2. CJM

    Jeff Cochran Guest

    On Fri, 19 Nov 2004 16:33:30 -0000, "CJM"
    <> wrote:

    >I'm trying to retrieve the users email address via ADSI in ASP.
    >
    >So far, I have the following code:
    >
    >Set oUser = GetObject("WinNT://domain/" & Request.Form("UID") & ",user")
    >Response.Write oUser.EmailAddress
    >
    >But I get the following message:
    >
    >"The directory property cannot be found in the cache. "
    >
    >Any ideas where I am going wrong?


    First guess is you're looking for a directory property that isn't in
    the cache... :)

    You're going to need to get specific and tell us versions of OS,
    Active Directory or not, etc. Also, if the email address isn't in
    your directory, you're still not going to get anything.

    Jeff
     
    Jeff Cochran, Nov 19, 2004
    #2
    1. Advertising

  3. CJM wrote:

    > I'm trying to retrieve the users email address via ADSI in ASP.
    >
    > So far, I have the following code:
    >
    > Set oUser = GetObject("WinNT://domain/" & Request.Form("UID") & ",user")
    > Response.Write oUser.EmailAddress
    >
    > But I get the following message:
    >
    > "The directory property cannot be found in the cache. "
    >
    > Any ideas where I am going wrong?


    Hi,

    I find that EmailAddress is not supported by the WinNT provider. You need to
    uses the LDAP provider.

    EmailAddress is actually a property method that returns the value of the
    mail attribute (which also is not supported by WinNT).

    --
    Richard
    Microsoft MVP Scripting and ADSI
    HilltopLab web site - http://www.rlmueller.net
    --
     
    Richard Mueller [MVP], Nov 19, 2004
    #3
  4. CJM

    CJM Guest

    "Jeff Cochran" <> wrote in message
    news:...
    > On Fri, 19 Nov 2004 16:33:30 -0000, "CJM"
    > <> wrote:
    >
    >>I'm trying to retrieve the users email address via ADSI in ASP.
    >>
    >>So far, I have the following code:
    >>
    >>Set oUser = GetObject("WinNT://domain/" & Request.Form("UID") & ",user")
    >>Response.Write oUser.EmailAddress
    >>
    >>But I get the following message:
    >>
    >>"The directory property cannot be found in the cache. "
    >>
    >>Any ideas where I am going wrong?

    >
    > First guess is you're looking for a directory property that isn't in
    > the cache... :)
    >
    > You're going to need to get specific and tell us versions of OS,
    > Active Directory or not, etc. Also, if the email address isn't in
    > your directory, you're still not going to get anything.
    >
    > Jeff


    Jeff,

    My development server is XPSP2 and we are using Active Directory 2003
    (Native mode).

    All the email address are populated.

    Thanks

    Chris
     
    CJM, Nov 23, 2004
    #4
  5. CJM

    CJM Guest

    "Richard Mueller [MVP]" <> wrote in
    message news:%...
    >
    > Hi,
    >
    > I find that EmailAddress is not supported by the WinNT provider. You need
    > to
    > uses the LDAP provider.
    >
    > EmailAddress is actually a property method that returns the value of the
    > mail attribute (which also is not supported by WinNT).
    >
    > --
    > Richard


    Thanks Richard, but can you point me in the right direction for the LDAP
    code. I gather its more involved than just swapping 'WinNT' for 'LDAP' - a
    different string entirely?

    Cheers

    Chris
     
    CJM, Nov 23, 2004
    #5
  6. CJM

    Jeff Cochran Guest

    On Tue, 23 Nov 2004 12:39:53 -0000, "CJM"
    <> wrote:

    >
    >"Jeff Cochran" <> wrote in message
    >news:...
    >> On Fri, 19 Nov 2004 16:33:30 -0000, "CJM"
    >> <> wrote:
    >>
    >>>I'm trying to retrieve the users email address via ADSI in ASP.
    >>>
    >>>So far, I have the following code:
    >>>
    >>>Set oUser = GetObject("WinNT://domain/" & Request.Form("UID") & ",user")
    >>>Response.Write oUser.EmailAddress
    >>>
    >>>But I get the following message:
    >>>
    >>>"The directory property cannot be found in the cache. "
    >>>
    >>>Any ideas where I am going wrong?

    >>
    >> First guess is you're looking for a directory property that isn't in
    >> the cache... :)
    >>
    >> You're going to need to get specific and tell us versions of OS,
    >> Active Directory or not, etc. Also, if the email address isn't in
    >> your directory, you're still not going to get anything.
    >>
    >> Jeff

    >
    >Jeff,
    >
    >My development server is XPSP2 and we are using Active Directory 2003
    >(Native mode).
    >
    >All the email address are populated.


    In AD? If so, you'll want to use LDAP to retrieve them instead of
    WMI.

    Jeff
     
    Jeff Cochran, Nov 23, 2004
    #6
  7. CJM

    Jeff Cochran Guest

    On Tue, 23 Nov 2004 12:42:03 -0000, "CJM"
    <> wrote:

    >
    >"Richard Mueller [MVP]" <> wrote in
    >message news:%...
    >>
    >> Hi,
    >>
    >> I find that EmailAddress is not supported by the WinNT provider. You need
    >> to
    >> uses the LDAP provider.
    >>
    >> EmailAddress is actually a property method that returns the value of the
    >> mail attribute (which also is not supported by WinNT).
    >>
    >> --
    >> Richard

    >
    >Thanks Richard, but can you point me in the right direction for the LDAP
    >code. I gather its more involved than just swapping 'WinNT' for 'LDAP' - a
    >different string entirely?


    Lots of sampels availble, try:

    http://www.4guysfromrolla.com/webtech/041800-1.shtml

    Jeff
     
    Jeff Cochran, Nov 23, 2004
    #7
  8. CJM

    CJM Guest

    Here's the solution I ended up with:

    <%@language="vbscript" %>
    <%
    Option Explicit

    Dim objRootDSE, strDNSDomain, objConnection, strQuery
    Dim objRecordSet, strName, strDN
    Dim strBase, strFilter, strAttributes

    ' Determine DNS domain name from RootDSE object.
    Set objRootDSE = GetObject("LDAP://RootDSE")
    strDNSDomain = objRootDSE.Get("defaultNamingContext")

    ' Use ADO to search Active Directory.
    Set objConnection = CreateObject("ADODB.Connection")
    objConnection.Provider = "ADsDSOObject"
    objConnection.Properties("User ID") = "domain\username"
    objConnection.Properties("Password") = "password"
    objConnection.Open "Active Directory Provider"

    ' Search for all user objects. Sort recordset by DisplayName.
    strBase = "<LDAP://" & strDNSDomain & ">"
    strFilter = "(&(objectCategory=person)(objectClass=user)(sAMAccountName=" &
    Session("UID") & "))"
    strAttributes = "mail"
    strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"

    Set objRecordSet = objConnection.Execute(strQuery)

    If objRecordSet.EOF Then
    response.Write "bolox"
    End If

    ' Loop through results
    Do Until objRecordSet.EOF
    response.Write "Email Address: " & objRecordSet.Fields("mail")
    objRecordSet.MoveNext
    Loop

    ' Clean up.
    objConnection.Close
    Set objRootDSE = Nothing
    Set objConnection = Nothing
    Set objRecordSet = Nothing

    %>


    I was told that the mail property is not always reliable, ie may not be
    populated, and that the proxyaddresses field would be better, but that
    returns a multi-value responsew which I can't seem to handle. Besides, mail
    seems to work OK for me...

    Thanks

    Chris
     
    CJM, Nov 23, 2004
    #8
  9. Hi,

    Some comments.

    1. If you know "mail" is populated, use it.
    2. You don't need to specify a userID and password unless you are running
    the code with an account that does not have permisssion to read the
    attributes.
    3. To read a multi-valued attribute, you can loop through the collection
    with a "For Each" loop. However, you must account for the possibility that
    the attribute will have no values. For example:

    ' Retrieve a multi-valued attribute .
    strAttributes = "proxyAddresses"

    Do Until adoRecordset.EOF
    colMail = adoRecordset.Fields("proxyAddresses")
    If IsEmpty(colMail) Then
    ' The attribute is empty (has no values).
    Response.Write "No Email address"
    Else
    ' The attribute has one or more values - enumerate.
    For Each strMail In colMail
    Response.Write "Email Address: " & strMail
    Next
    End If
    Loop

    --
    Richard
    Microsoft MVP Scripting and ADSI
    HilltopLab web site - http://www.rlmueller.net
    --

    "CJM" <> wrote in message
    news:...
    > Here's the solution I ended up with:
    >
    > <%@language="vbscript" %>
    > <%
    > Option Explicit
    >
    > Dim objRootDSE, strDNSDomain, objConnection, strQuery
    > Dim objRecordSet, strName, strDN
    > Dim strBase, strFilter, strAttributes
    >
    > ' Determine DNS domain name from RootDSE object.
    > Set objRootDSE = GetObject("LDAP://RootDSE")
    > strDNSDomain = objRootDSE.Get("defaultNamingContext")
    >
    > ' Use ADO to search Active Directory.
    > Set objConnection = CreateObject("ADODB.Connection")
    > objConnection.Provider = "ADsDSOObject"
    > objConnection.Properties("User ID") = "domain\username"
    > objConnection.Properties("Password") = "password"
    > objConnection.Open "Active Directory Provider"
    >
    > ' Search for all user objects. Sort recordset by DisplayName.
    > strBase = "<LDAP://" & strDNSDomain & ">"
    > strFilter = "(&(objectCategory=person)(objectClass=user)(sAMAccountName="

    &
    > Session("UID") & "))"
    > strAttributes = "mail"
    > strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
    >
    > Set objRecordSet = objConnection.Execute(strQuery)
    >
    > If objRecordSet.EOF Then
    > response.Write "bolox"
    > End If
    >
    > ' Loop through results
    > Do Until objRecordSet.EOF
    > response.Write "Email Address: " & objRecordSet.Fields("mail")
    > objRecordSet.MoveNext
    > Loop
    >
    > ' Clean up.
    > objConnection.Close
    > Set objRootDSE = Nothing
    > Set objConnection = Nothing
    > Set objRecordSet = Nothing
    >
    > %>
    >
    >
    > I was told that the mail property is not always reliable, ie may not be
    > populated, and that the proxyaddresses field would be better, but that
    > returns a multi-value responsew which I can't seem to handle. Besides,

    mail
    > seems to work OK for me...
    >
    > Thanks
    >
    > Chris
    >
    >
    >
     
    Richard Mueller [MVP], Nov 23, 2004
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andy

    Retrieving client's MAC address

    Andy, May 28, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    13,252
    Kevin Spencer
    May 29, 2005
  2. tangus via DotNetMonster.com

    Problem retrieving Active Directory users

    tangus via DotNetMonster.com, Sep 30, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    2,825
    S. Justin Gengo
    Oct 1, 2005
  3. rock69
    Replies:
    3
    Views:
    440
  4. Zeynel
    Replies:
    1
    Views:
    585
    alex23
    Dec 6, 2010
  5. Replies:
    5
    Views:
    277
    Dr John Stockton
    Dec 23, 2005
Loading...

Share This Page