Re: LDAP, Email



sAMAccountName is unqualified in AD, so you would need to remove the domain
part. If you don't know which domain the user is in, then it gets more
complicated. :)

Also, don't do this:

If Not IsNothing(res) Then
Dim deUser As DirectoryEntry = res.GetDirectoryEntry()
strEmail = deUser.Properties("mail").Value.ToString()
End If

It makes your code slow. You've already retrieved the email address in the
SearchResult and then you go and create another DirectoryEntry just to read
it, resulting in more searches and network chatter. Do this instead:

If Not IsNothing(res) Then
If res.Properties.Contains("mail") Then
strEmail = DirectCast(res.Properties("mail")(0), String)
End If
End If

That will use the data you already have in memory from your search.

Joe K.


"Dianna" <Dianna@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:387EAAD4-9D37-4682-8EAB-DD52A0B7EF57@xxxxxxxxxxxxxxxx
Ok, I found what my problem was in my original code: the username.
This code works. However, my Username that I was passing in for the
filter
samAccountName was qualified: Domain/UserID. When I removed the domain
from
the Username it worked. Is this correct? Now I guess I have to chop off
the
domain/ from all the users i use this function for.


Dim oDS As New DirectorySearcher(_path & _domain)
oDS.Filter = "(samAccountName=" + Username + ")"
oDS.PropertiesToLoad.Add("mail")
Dim res As SearchResult = oDS.FindOne()
Dim strEmail As String

If Not IsNothing(res) Then
Dim deUser As DirectoryEntry = res.GetDirectoryEntry()
strEmail = deUser.Properties("mail").Value.ToString()
End If


"Dianna" wrote:

Thanks Joe and Henning,

I will look further into this.

"Joe Kaplan (MVP - ADSI)" wrote:

It is a little cleaner to actually specify the default naming context
of the
domain as the search root explicitly instead of trying to rely on ADSI
to
figure that out for you. I'd also recommend against using NETBIOS
names for
AD. DNS is always better. I'd do this (works in 1.1 or 2.0; 2.0 has
other
better options as well):

Dim rootDSE as DirectoryEntry = New
DirectoryEntry("LDAP://abc.xxx.xxx/RootDSE";)
dnc = DirectCast(rootDSE.Properties("defaultNamingContext").Value,
String)

oDS = New DirectoryEntry("LDAP://abc.xxx.xxx/"; + dnc)

And proceed from there.

Another thing to consider is that you are using default credentials in
your
bind. If your current security context is not a domain identity or
cannot
be delegated, you might be authenticating with the server as an
anonymous
user and might therefore not have permissions to see anything. If you
suspect that is true, try changing your DirectoryEntry constructor to
supply
some know good credentials for testing. If that fixes it, then you
know you
have a security problem.

Joe K.


"Dianna" <Dianna@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:71E8401D-7DD8-4C6A-A395-E8141BBC425B@xxxxxxxxxxxxxxxx
Hi Henning,
_path = LDAP://
_domain = ABC
so it looks like LDAP://ABC

The user name is the Windows it ex: ABC/jsmith



"Dianna" wrote:

Hi,

I am trying to retrieve the email from AD for a specific user, I am
getting
no results. The below code sends in a username that has an email
address:

Dim oDS As New DirectorySearcher(_path & _domain)
oDS.Filter = "(samAccountName=" + Username + ")"
oDS.PropertiesToLoad.Add("mail")
Dim res As SearchResult = oDS.FindOne()
Dim strEmail As String

If Not IsNothing(res) Then
Dim deUser As DirectoryEntry =
res.GetDirectoryEntry()
strEmail =
deUser.Properties("mail").Value.ToString()
End If
What am I doing wrong. I'm very new to LDAP, but the above seems
simple
enough...?
Thanks,
Dianna






.



Relevant Pages

  • Re: LDAP and Organization group
    ... DirectoryEntry obj and pased the path, as well as username and pw to it. ... > specify credentials with your DirectoryEntry unless you want your password ... >> Dim searchResult As SearchResult ... >> Dim mySearchResultColl As SearchResultCollection ...
    (microsoft.public.dotnet.security)
  • Re: LDAP, Email
    ... SearchResult and then you go and create another DirectoryEntry just to read ... strEmail = DirectCast, String) ... Dim oDS As New DirectorySearcher ...
    (microsoft.public.dotnet.security)
  • Re: Get SAMAccountNames for all users in an active directory group
    ... what is the point of the impersonation code? ... Dim arrListSAMAccounts As New ArrayList ... Dim searchRoot As DirectoryEntry = Nothing ...
    (microsoft.public.windows.server.active_directory)
  • Re: Create an email group and send report(s)
    ... help from the community members here. ... Dim rst As DAO.Recordset ... Dim strEmail As String ... they select the report to attach and they select the recipients to send it to. ...
    (microsoft.public.access.forms)
  • Re: Email Subject Line
    ... Function ForecastnotificationFC(strOpCo As String, strEmail As String) As ... Dim strRecipient As String ... Call MsgBox(_ ...
    (microsoft.public.access.modulesdaovba)