I use this code for find users in AD
SearchResultCollection results = null;
try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
System.DirectoryServices.DirectoryEntry entry = new DirectoryEntry("LDAP://...");
DirectorySearcher adSearcher = new DirectorySearcher(entry);
adSearcher.SearchScope = SearchScope.Subtree;
adSearcher.Filter = "(&(objectClass=user)(sAMAccountName=" + userName + "))";
results = adSearcher.FindAll();
});
}
catch (Exception e)
{
}
and add a Children
using (DirectoryEntry ou = new DirectoryEntry("LDAP://Domainname"))
{
DirectoryEntry user = ou.Children.Add("CN=" + txtuser.Text, "user");
int NORMAL_ACCOUNT = 0x200;
int PWD_NOTREQD = 0x20;
user.Properties["sAMAccountName"].Value = txtuser.Text;
user.Properties["userAccountControl"].Value = NORMAL_ACCOUNT | PWD_NOTREQD;
user.Properties["displayName"].Value = txtname.Text;
user.Properties["mail"].Value=txtemail.TemplateControl;
user.Properties["description"].Value = txtnote.Text;
user.CommitChanges();
user.Invoke("SetPassword", new object[] { txtpass.Text });
}
How do modify active directory user properties?