Pulling user data from Active Directory

  • Thread starter Thread starter Joe Spears
  • Start date Start date
J

Joe Spears

Hi
Does anyone have any sample code on Pulling user data from Active
Directory??

Thanks
 
This is done using the System.DirectoryServices namespace. You can get
information about the namespace here:

http://msdn.microsoft.com/library/d...s/cpref/html/frlrfsystemdirectoryservices.asp

Specifically, you would use the LDAP protocol with a
System.DirectoryServices.DirectoryEntry instance. You can read up on this
class at:

http://msdn.microsoft.com/library/d...ctoryservicesdirectoryentryclassctortopic.asp

The documentation for the constructor has some nice samples:

http://msdn.microsoft.com/library/e...esdirectoryentryclassctortopic.asp?frame=true

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

If the truth hurts, wear it.
 
Here is a sample:

using System.DirectoryServices;
using System.Security.Principal;


public void LearnLDAPUserQuery()
{
string userPath = "CN=my user,OU=Users and Computers,DC=corp";
string ldapPrefix = "LDAP://";

DirectoryEntry userEntry = new DirectoryEntry(ldapPrefix +
userPath);

Console.WriteLine(userEntry.Path);
Console.WriteLine(userEntry.SchemaClassName);
foreach (string propName in userEntry.Properties.PropertyNames)
{
Console.WriteLine("{0}: {1}", propName,
userEntry.Properties[propName].Value);

}

dumpActiveDirectorySecurity(userEntry.ObjectSecurity);
Console.WriteLine("logonCount: {0}",
userEntry.Properties["logonCount"].Value);
}

private void dumpActiveDirectorySecurity(ActiveDirectorySecurity
security)
{
NTAccount groupAccount = security.GetGroup(typeof(NTAccount)) as
NTAccount;
Console.WriteLine("User belongs to group: {0}",
groupAccount.Value);
}
 
Back
Top