Click here to Skip to main content
15,748,748 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I try to get email address from AD by using System.DirectoryServices.AccountManagement.UserPrincipal.Current.EmailAddress.
It works in my local (VS2008) but I got error 'system.DirectoryServices.AccountManagement.GroupPrincipal' to type 'System.DirectoryServices.AccountManagement.UserPrincipal' when I try it on my web server.
I am using IIS 6.0 with virtual directory and I have set it to window authentication on the virtual directory.
Also in my web.config, I set <authentication mode="Windows"> and added the site to my local intranet sites.

Any help is greatly appreciated.
Posted

1 solution

The user credentials specified to connect to the AD from in code needs admin permissions. Check this is true.
 
Share this answer
 
Comments
snoopy18 7-Nov-12 1:30am    
Do you by any chance have sample code? This is an intranet application so they are all domain users and should have read access right to AD. Thanks in advance.
njammy 7-Nov-12 4:33am    
That is true, but just like an application needs a connection string login to a database, it needs one for AD as well.

See below

<pre>public Dictionary<String, String> GetContacts()
{
Dictionary<String, String> contactList = new Dictionary<String, String>();

DirectoryEntry entry = new DirectoryEntry("LDAP://yourdomain/OU=UserGroupName,OU=Users,DC=domain,DC=local");
entry.Username = "adminUserName"; //<-- This is your full admin rights user for connection to AD
entry.Password = "adminPassword";

DirectorySearcher searcher = new DirectorySearcher(entry);

searcher.Filter = "(&(objectClass=user))";
searcher.PropertiesToLoad.Add("sAMAccountName");

SearchResultCollection result = searcher.FindAll();

String loginName = String.Empty,
firstName = String.Empty,
surname = String.Empty,
mail = String.Empty;

try
{
foreach (SearchResult userObject in result)
{
if (userObject != null)
{
PropertyCollection props = userObject.GetDirectoryEntry().Properties;

if (!Convert.ToBoolean((int)props["userAccountControl"].Value & 0x0002))
{
// Get the user's login name and firstname.
// Only get the surname and mail address if surname != null

if (props["sAMAccountName"].Value != null)
{
loginName = props["sAMAccountName"].Value.ToString();
}

if (props["givenname"].Value != null)
{
firstName = props["givenname"].Value.ToString();
}



if (props["sn"].Value != null)
{
surname = " " + props["sn"].Value.ToString();
}

// This line gets the email value
if (props["mail"].Value != null)
{
mail = props["mail"].Value.ToString();
}

contactList.Add(firstName + surname, mail);

}
}
}

if (contactList.Count > 0)
{
var sortedList = (from item in contactList orderby item.Value ascending select item);
contactList = sortedList.ToDictionary(sourceKeyValuePair => sourceKeyValuePair.Key, sourceKeyValuePair => sourceKeyValuePair.Value);
}
}
catch { }
return contactList;
}</pre>
snoopy18 7-Nov-12 11:49am    
Thank you for the quick response. I will try to translate it to vb.net and test it. Thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900