Forum guidelines
AuthorQuestion
Get Active Directory in C#.Net
Posted By Ravindranath M on 15 Dec 2012

  Hi..

  How to get Phone Number, Email Id from Active Directory which is present in Network..
  Send Some Sample  working Code as an attachment

AuthorReply
Re: Get Active Directory in C#.Net
Posted By mahendra bisht on 01 Sep 2013  

DirectoryEntry dirEntry = new DirectoryEntry();
dirEntry .Path = ConfigurationManager.AppSettings[AD_SERVER] + ConfigurationManager.AppSettings[AD_DOMAIN_NAME];
dirEntry .Username = ConfigurationManager.AppSettings[AD_ADMIN_USER];
dirEntry .Password = ConfigurationManager.AppSettings[AD_ADMIN_PASSWORD];
dirEntry .AuthenticationType = AuthenticationTypes.Secure;
 
 
//create instance fo the direcory searcher
DirectorySearcher dirSearch = new DirectorySearcher(dirEntry);
dirSearch.SearchRoot = dirEntry;
//set the search filter    
dirSearch.Filter = "(&(objectClass=user)(cn=" + UserName + "))";
 
//find the first instance
SearchResult searchResults = dirSearch.FindOne();
//if found then return, otherwise return Null
if ((searchResults != null))
{
 
 //if so then return the DirectoryEntry object 
 DirectoryEntry user =  searchResults.GetDirectoryEntry();

}

string mail=user.Properties[MAIL].Value








Re: Get Active Directory in C#.Net
Posted By mahendra bisht on 01 Sep 2013  

DirectoryEntry dirEntry = new DirectoryEntry();
dirEntry .Path = ConfigurationManager.AppSettings[AD_SERVER] + ConfigurationManager.AppSettings[AD_DOMAIN_NAME];
dirEntry .Username = ConfigurationManager.AppSettings[AD_ADMIN_USER];
dirEntry .Password = ConfigurationManager.AppSettings[AD_ADMIN_PASSWORD];
dirEntry .AuthenticationType = AuthenticationTypes.Secure;
 
 
//create instance fo the direcory searcher
DirectorySearcher dirSearch = new DirectorySearcher(dirEntry);
dirSearch.SearchRoot = dirEntry;
//set the search filter    
dirSearch.Filter = "(&(objectClass=user)(cn=" + UserName + "))";
 
//find the first instance
SearchResult searchResults = dirSearch.FindOne();
//if found then return, otherwise return Null
if ((searchResults != null))
{
 
 //if so then return the DirectoryEntry object 
 DirectoryEntry user =  searchResults.GetDirectoryEntry();

}

string mail=user.Properties["mail"].Value


SPONSORED BY

Custom Software Development
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.