Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
System.DirectoryServices class will help finding the information of a Domain User.
Function which will help retriving Active Directory Object:
private DirectoryEntry GetActiveDirectoryObject(string filter, string ___domain, string[] properties)
{
/// Running LADAP query to get the directory entry
DirectoryEntry adsRoot = new DirectoryEntry("LDAP://" + ___domain, null, null, AuthenticationTypes.Secure);
DirectorySearcher searcher = new DirectorySearcher(adsRoot);
searcher.SearchScope = SearchScope.Subtree;
searcher.ReferralChasing = ReferralChasingOption.All;
searcher.PropertiesToLoad.AddRange(properties);
searcher.Filter = filter;
SearchResult result = searcher.FindOne();
DirectoryEntry directoryEntry = result.GetDirectoryEntry();
return directoryEntry;
}
Parameters one has to pass to GetActiveDirectoryObject funtion:
string filter = string.Format("(&(ObjectClass={0})(sAMAccountName={1}))","person", username);
string ___domain = Current User's Domain
string[] properties = One can pass string array of properties, for Example: string[] properties = new string[] { "memberOf" };
Retriving data from the DirectoryEntry object:
DirectoryEntry directoryEntry = GetActiveDirectoryObject(filter, ___domain, properties);
if (directoryEntry != null)
{
directoryEntry.AuthenticationType = AuthenticationTypes.None;
string displayName = directoryEntry.Properties["displayName"][0].ToString();
string firstName = directoryEntry.Properties["givenName"][0].ToString();
string lastName = directoryEntry.Properties["sn"][0].ToString();
string email = directoryEntry.Properties["mail"][0].ToString();
}
I have also attached the sample code. For more information on the System.DirectoryServices please refere MSDN, it has detailed description about it.
Cheers!!!!