Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static void Main(string[] args)
- {
- SearchResultCollection sResults = null;
- try
- {
- //modify this line to include your domain name
- string path = "LDAP://EXTECH";
- //init a directory entry
- DirectoryEntry dEntry = new DirectoryEntry(path);
- //init a driectory searcher
- DirectorySearcher dSearcher = new DirectorySearcher(dEntry);
- //This line applies a filter to the search specifying a username to search for
- //modify this line to specify a user name. if you want to search for all
- //users who start with k - set SearchString to "k"
- dSearcher.Filter = "(&(objectClass=user))";
- //perform search on active directory
- sResults = dSearcher.FindAll();
- //loop through results of search
- foreach (SearchResult searchResult in sResults)
- {
- if (searchResult.Properties["CN"][0].ToString() == "Adit")
- {
- ////loop through the ad properties
- //foreach (string propertyKey in
- //searchResult.Properties["st"])
- //{
- //pull the collection of objects with this key name
- ResultPropertyValueCollection valueCollection =
- searchResult.Properties["manager"];
- foreach (Object propertyValue in valueCollection)
- {
- //loop through the values that have a specific name
- //an example of a property that would have multiple
- //collections for the same name would be memberof
- //Console.WriteLine("Property Name: " + valueCollection..ToString());
- Console.WriteLine("Property Value: " + (string)propertyValue.ToString());
- //["sAMAccountName"][0].ToString();
- }
- //}
- Console.WriteLine(" ");
- }
- }
- }
- catch (InvalidOperationException iOe)
- {
- //
- }
- catch (NotSupportedException nSe)
- {
- //
- }
- finally
- {
- // dispose of objects used
- if (sResults != null)
- sResults.Dispose();
- }
- Console.ReadLine();
- }
- dSearcher.Filter = "(&(objectClass=user))";
- dSearcher.Filter = "(&(objectClass=user)(cn=Adit))";
- DirectorySearcher searcher = new DirectorySearcher();
- searcher.Filter = "(&(objectCategory=user)(cn=steve.evans))";
- SearchResultCollection results = searcher.FindAll();
- if (results.Count == 1)
- {
- //do what you want to do
- }
- else if (results.Count == 0)
- {
- //user does not exist
- }
- else
- {
- //found more than one user
- //something is wrong
- }
- DirectoryEntry deUser = new DirectoryEntry("LDAP://cn=John Doe,cn=Users,dc=yourdomain,dc=com");
- if (deUser != null)
- {
- ... do something with your user
- }
- // bind to your domain
- PrincipalContext pc = new PrincipalContext(ContextType.Domain, "LDAP://dc=yourdomain,dc=com");
- // find the user by identity (or many other ways)
- UserPrincipal user = UserPrincipal.FindByIdentity(pc, "cn=John Doe");
Add Comment
Please, Sign In to add comment