using System; using System.DirectoryServices; public class SimpleSearch { public static void Main() { DirectoryEntry root = new DirectoryEntry( "LDAP://192.168.1.100/DC=ispnet1,DC=net", "cn=Administrator, dc=ispnet1, dc=net", "password", AuthenticationTypes.ServerBind); DirectorySearcher searcher = new DirectorySearcher(root); searcher.Filter = "(&(objectClass=person)(sn=Blum))"; searcher.PropertiesToLoad.Add("cn"); searcher.PropertiesToLoad.Add("telephoneNumber"); SearchResultCollection results = searcher.FindAll(); foreach(SearchResult result in results) { string searchpath = result.Path; Console.WriteLine("path: {0}", searchpath); ResultPropertyCollection rpc = result.Properties; foreach(string property in rpc.PropertyNames) { foreach(object value in rpc[property]) Console.WriteLine(" property = {0} value = {1}", property, value); } } } }
Web Services
DirectoryEntry Rename Object
using System; using System.DirectoryServices; public class RenameObject { public static void Main() { DirectoryEntry de = new DirectoryEntry( "LDAP://192.168.1.100/ou=auditing, ou=accounting, dc=ispnet1, dc=net", "cn=Administrator, dc=ispnet1, dc=net", "password", AuthenticationTypes.ServerBind); DirectoryEntries des = de.Children; DirectoryEntry badObject = des.Find("cn=test"); badObject.Rename("cn=testing"); de.CommitChanges(); de.Close(); } }
DirectoryEntry and DirectoryEntries
using System; using System.DirectoryServices; public class RemoveObject { public static void Main() { DirectoryEntry de = new DirectoryEntry( "LDAP://192.168.1.100/ou=accounting, dc=ispnet1, dc=net", "cn=Administrator, dc=ispnet1, dc=net", "password", AuthenticationTypes.ServerBind); DirectoryEntries children = de.Children; try { DirectoryEntry badObject = children.Find("ou=auditing"); children.Remove(badObject); de.CommitChanges(); Console.WriteLine("the object was removed"); } catch (Exception) { Console.WriteLine("the object was not found"); } } }
DirectoryServices:Modify Property
using System; using System.DirectoryServices; public class ModifyProperty { public static void Main() { DirectoryEntry de = new DirectoryEntry( "LDAP://192.168.1.100/cn=kblum, ou=sales, dc=ispnet1, dc=net", "cn=Administrator, dc=ispnet1, dc=net", "password", AuthenticationTypes.ServerBind); de.Properties["sn"][0] = "Mullen"; de.CommitChanges(); Console.WriteLine("New property value: {0}", de.Properties["sn"][0]); de.Close(); } }
DirectoryEntry : List Objects
using System; using System.Collections; using System.DirectoryServices; public class ListObjects { public static void Main() { DirectoryEntry de = new DirectoryEntry( "LDAP://192.168.1.100/dc=ispnet1, dc=net"); Console.WriteLine(de.Path); DirectoryEntries des = de.Children; foreach(DirectoryEntry entry in des) { Console.WriteLine(" child: " + entry.Name); } } }
retrieves Active Directory information
using System; using System.DirectoryServices; public class Example21_16 { public static void Main() { // connect to AD DirectoryEntry de = new DirectoryEntry( "WinNT://DomanName/MachineName", "Administrator", "Password"); foreach(DirectoryEntry child in de.Children) { Console.WriteLine(child.SchemaClassName + ": " + child.Name); } } }
DirectoryServices DirectoryEntry
using System; using System.Net; using System.DirectoryServices; using System.DirectoryServices.Protocols; public class MainClass { public static void Main() { using (DirectoryEntry de = new DirectoryEntry()) { de.Path = "LDAP://yourSite/rootDSE"; de.Username = @"explorerchris"; de.Password = "password"; PropertyCollection props = de.Properties; foreach (string prop in props.PropertyNames) { PropertyValueCollection values = props[prop]; foreach (string val in values) { Console.Write(prop + ": "); Console.WriteLine(val); } } } } }