using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.DirectoryServices;//you must add this reference into this solution firstly
namespace ADTest
{
class Program
{
public struct UserInfo
{
//Required Elements for my AD Implementation
public string username;//AD Attribute Name: CN
public string sAMAccountName;//AD Attribute Name:SAMAccountName
//Optional Elements
public string password;
public string firstName;//AD Attribute Name:givenName
public string initials;//AD Attribute Name:Initials
public string lastName;//AD Attribute Name:SN
public string displayName;//AD Attribute Name:displayName
public string officeName;//AD Attribute Name:physicalDeliveryOfficeName
public string telephoneNumber;//AD Attribute Name:telephoneNumber
public string emailAddress;//AD Attribute Name:mail
}
static void Main(string[] args)
{
#region Search in AD
// Step#1: Instantiate a new DirectoryEntry object that will represent
// the folder in which I want to start my search, or blank if i
// want to start at the top of the AD structure
DirectoryEntry adFolderObject = new DirectoryEntry();
// Step#2: Instantiate a DirectorySearcher object and reference the
// folder object that will be the base for our search (see step #1)
DirectorySearcher adSearcherObject = new DirectorySearcher(adFolderObject);
// Step#3: Set the Scope of our search
// (option:Base, OneLevel,or Subtree)
// See resources slide for more details
adSearcherObject.SearchScope = SearchScope.Subtree;
// Step#4: Set the filter for our search, based on AD Search Syntax (See resources slide)
// Examples:
// -All users
// adUsers.Filter="(ObjectClass=User)"
// -All users, except built-in accounts
// adUsers.Filter="(&(ObjectClass=User)(idescription=Built-in))"
// -All users in the sales office
// adUsers.Filter="(&(ObjectClass=User)(physicalDeliveryOfficeName=Sales))"
adSearcherObject.Filter = "(ObjectClass=user)";
// Step#5:Execute the search and iterate throught the result set and in
// this case, show the Common Name (CN) and Path of the adObjects
// that we searched for.
foreach (SearchResult adObject in adSearcherObject.FindAll())
{
Console.WriteLine("CN={0},Path={1}",adObject.Properties["CN"][0],adObject.Path);
}
Console.WriteLine();
Console.WriteLine("Hit any key to continue...");
Console.ReadLine();
#endregion
#region Add user into AD
//Step #0:Setting the values for the new user that we are going to add
UserInfo newUserInfo;
newUserInfo.firstName = "John";
newUserInfo.lastName = "Smith";
newUserInfo.initials = "JS";
newUserInfo.displayName = "John Smith";
newUserInfo.emailAddress = "
[email protected]";
newUserInfo.officeName = "Sales";
newUserInfo.telephoneNumber = "(555)123-4567";
//Require values
newUserInfo.username = "JSmith";//This will be the CN,which is always Required
newUserInfo.sAMAccountName = "JSmith"; //This is required for mixed domains
//meaning windows 2003 with pre widows 2000
//Step #1:Instantiate a new DirectoryEntry Object to repesent the user folder within AD
DirectoryEntry adUserFolder = new DirectoryEntry("LDAP://CN=Users,DC=RSLab,DC=local");
//Step #2 Make sure the folder is container folder, Users can only be add to container folder
if(adUserFolder.SchemaEntry.Name=="container")
{
#region Add Group into AD and add user into group
DirectoryEntry newGroup = adUserFolder.Children.Add("CN=Sales Group ", "Group");
if (DirectoryEntry.Exists(newGroup.Path))
{
Console.WriteLine("The Group is exist, it will be deleted");
adUserFolder.Children.Remove(new DirectoryEntry(newGroup.Path));
}
newGroup.Properties["sAMAccountName"].Value = "Sales Group";//Required for my AD implementation
newGroup.Properties["groupType"].Value = 4;//4= Domain Local group [required]
newGroup.Properties["description"].Value = "The Sales Group";//[optional]
newGroup.Properties["mail"].Value = "
[email protected]";//[optional]
newGroup.Properties["managedBy"].Value =
new DirectoryEntry("LDAP://Peter PumkinEater,CN=User,DC=RSLab,DC=local")
.Properties["distingushedName"].Value;//[optional]
newGroup.CommitChanges();
Console.WriteLine("New Group created...");
Console.WriteLine("Add Peter PumkinEater to the sales group");
newGroup.Invoke("Add", "LDAP://Peter PumkinEater,CN=User,DC=RSLab,DC=local");
newGroup.Invoke("Add", "LDAP://John Smith,CN=User,DC=RSLab,DC=local");
newGroup.Invoke("Add", "LDAP://Ken Liu,CN=User,DC=RSLab,DC=local");
#endregion
//Step #3 Instantiate a DirectoryEntry Ojbect to represent the new user
DirectoryEntry newUser = adUserFolder.Children.Add("CN="+ newUserInfo.username, "User");
//Step #4 Check if the user object exist, if yes, we will delete it
if(DirectoryEntry.Exists(newUser.Path))
{
Console.WriteLine("The user" + newUser.Username + " exist, it will be deleted");
adUserFolder.Children.Remove(new DirectoryEntry(newUser.Path));
}
//Step #5 Setting the AD attributes to new user, and then MOST IMPORTANT committing the changes to AD
newUser.Properties["sAMAccountName"].Value = newUserInfo.sAMAccountName;
newUser.Properties["givenName"].Value = newUserInfo.firstName;
newUser.Properties["sn"].Value = newUserInfo.lastName;
newUser.Properties["initials"].Value = newUserInfo.initials;
newUser.Properties["displayName"].Value = newUserInfo.displayName;
newUser.Properties["physicalDeliveryOfficeName"].Value = newUserInfo.officeName;
newUser.Properties["telephoneNumber"].Value = newUserInfo.telephoneNumber;
newUser.Properties["mail"].Value = newUserInfo.emailAddress;
//Lately we have to committe the changes
newUser.CommitChanges();
//Step #6 Setting password and enabling user account. we will use ADSI
//Invoke command to set the password and set the "userAccountControl"
//to enabled(value 0x2001)
newUser.Invoke("setpassword", "P@ssword1");
newUser.Properties["userAccountControl"].Value = 0x2001;
newUser.CommitChanges();
Console.WriteLine("The User" + newUser.Username + " Created successfully!");
Console.WriteLine("Hit any key to continue...");
Console.ReadLine();
}
#endregion
}
}
}