public void GetUserDistinguishedName(string sAMAccountName)
{
using (DirectoryEntry dirEntry = new DirectoryEntry())
{
dirEntry.Path = "LDAP://OU=OUName,DC=DCName,DC=org";
dirEntry.AuthenticationType = AuthenticationTypes.Secure;
using (DirectorySearcher dirSearch = new DirectorySearcher(dirEntry))
{
dirSearch.Filter = string.Format("(&(objectCategory=person)(objectClass=user)(SAMAccountName={0}))", sAMAccountName);
SearchResult result = dirSearch.FindOne();
if (result != null)
{
Console.WriteLine(string.Format("Distinguished Name: {0}" ,result.Properties["distinguishedname"][0].ToString()));
Console.WriteLine(string.Format("First Name: {0}", result.Properties["givenname"][0].ToString()));
Console.WriteLine(string.Format("Last Name: {0}", result.Properties["sn"][0].ToString()));
Console.WriteLine(string.Format("Email Address: {0}", result.Properties["mail"][0].ToString()));
}
}
}
}
Monday, September 10, 2012
Programmatically obtain user information from Active Directory
Here is a sample method that you can use for contacting Active Directory in order to obtain information about a user based on his/her username/account name (a.k.a. sAMAccountName in AD):
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.