|
retrieve user's email address from Active Directory -- dporter --
I am trying to get a user's email address based on a LDAP query to AD. I am able to connect to AD and retrieve properties for the user account, but can't get the email address for the life of me. I am using .net 1.1 and the sever is Windows Server 2003. Here is the code I'm using for the retrieval: DirectorySearcher objSearch = new DirectorySearcher(); objSearch.SearchRoot = new DirectoryEntry("LDAP://DC=*****,DC=com"); objSearch.Filter = "(&(objectClass=user)(objectCategory=person)(anr=bsmith))"; objSearch.PropertiesToLoad.Add("cn"); SearchResult sr = objSearch.FindOne(); Debug.WriteLine(sr.Properties .ToString()); I have tried the following (among many other things) to no avail: 1. replace "cn" with "mail" where it occurs - get a Object reference not set to an instance of an object error message. 2. added (objectCategory=mail) to the filter - got no output or above Object reference message. Can anyone tell me what I am doing wrong? According to the documentation I found, it shouldn't be this hard. I appreciate any help you can provide. Dennis |
|
-- BradleyPeter --
I guess you'll need to look in AD to find where the EMail address is stored. In our system it's somewhere completely idiotic like "Company Name" or something. If this topic is still live on Monday I'll look in our code and see how we do it. I haven't looked at that code for some time so I don't want to post anything misleading. Peter -----Original Message----- From: DotNetDevelopment on behalf of dporter Sent: Fri 7/21/2006 8:21 PM To: DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting Cc: Subject: retrieve user's email address from Active Directory I am trying to get a user's email address based on a LDAP query to AD. I am able to connect to AD and retrieve properties for the user account, but can't get the email address for the life of me. I am using .net 1.1 and the sever is Windows Server 2003. Here is the code I'm using for the retrieval: DirectorySearcher objSearch = new DirectorySearcher(); objSearch.SearchRoot = new DirectoryEntry("LDAP://DC=*****,DC=com"); objSearch.Filter = "(&(objectClass=user)(objectCategory=person)(anr=bsmith))"; objSearch.PropertiesToLoad.Add("cn"); SearchResult sr = objSearch.FindOne(); Debug.WriteLine(sr.Properties .ToString()); I have tried the following (among many other things) to no avail: 1. replace "cn" with "mail" where it occurs - get a Object reference not set to an instance of an object error message. 2. added (objectCategory=mail) to the filter - got no output or above Object reference message. Can anyone tell me what I am doing wrong? According to the documentation I found, it shouldn't be this hard. I appreciate any help you can provide. Dennis |
|
-- dporter --
Thanks for the response Peter. Glad to know I'm not going crazy. Yet. I looked at the user properties in AD and it has an E-mail field. I put "E-mail" in in place of "mail" and still get the above Object reference message. Is there another way to find out which field or field name I should be using? That is, how did you find the email address in the Company Name field? I appreciate your help, Dennis |
|
-- BradleyPeter --
Heh! The only way I know of is to talk to the guy who designed your AD database. That's what I did. What I found out was that the email address was in the company field, "So that it would appear in the global address book". So we're stuck with it. I also found that it appears in "Mail" and "Mail nickname". Remember you can call the nodes in AD anything you like and stick anything anywhere you like in each node. It's just a nested tree structure like a file system. If you store your last will and testament in a folder called "GasBills", no-one's going to stop you! :) HTH Peter -----Original Message----- From: DotNetDevelopment Re: retrieve user's email address from Active Directory Thanks for the response Peter. Glad to know I'm not going crazy. Yet. I looked at the user properties in AD and it has an E-mail field. I put "E-mail" in in place of "mail" and still get the above Object reference message. Is there another way to find out which field or field name I should be using? That is, how did you find the email address in the Company Name field? I appreciate your help, Dennis |
|
-- dporter --
Okay. I'll keep picking at it. Thanks so much for your help, Dennis |