Re: LDAP, Email
- From: "Joe Kaplan \(MVP - ADSI\)" <joseph.e.kaplan@xxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Thu, 6 Apr 2006 14:40:51 -0500
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
.
- Follow-Ups:
- Re: LDAP, Email
- From: Dianna
- Re: LDAP, Email
- References:
- Re: LDAP, Email
- From: Joe Kaplan \(MVP - ADSI\)
- Re: LDAP, Email
- From: Dianna
- Re: LDAP, Email
- From: Dianna
- Re: LDAP, Email
- Prev by Date: Re: LDAP, Email
- Next by Date: Re: LDAP, Email
- Previous by thread: Re: LDAP, Email
- Next by thread: Re: LDAP, Email
- Index(es):
Relevant Pages
|