Menu

Tuesday, March 20, 2007

Read active directory

Using this code you can read Active Directory of your domain. On this code segment I have retrieved some active directory properties and set them as session variables.


using System.DirectoryServices;

DirectoryEntry entry = new DirectoryEntry("LDAP://yourdomain.com");
DirectorySearcher searcher = new DirectorySearcher(entry);
searcher.Filter = "(SAMAccountName=" +User.Identity.Name.ToString().Replace("yourdomain\\", "") + ")";

SearchResult result = searcher.FindOne();
if (result != null)
{
Session["Surname"] = result.Properties["sn"][0];
Session["Firstname"] = result.Properties["givenname"][0];
Session["Email"] = result.Properties["mail"][0];
Session["Title"] = result.Properties["title"][0];

Response.Write(result.Properties["sn"][0] + " ");
Response.Write(result.Properties["givenname"][0] + " ");
Response.Write(result.Properties["mail"][0] + " ");
Response.Write(result.Properties["title"][0] + " ");
}

No comments: