Adding new Windows User Account using Active Directory


I used Active Directory to add new user account. The Active Directory will be accessed using windows native protocol (not LDAP).

The code is like following:

//active directory connection string
strConn = AUTHENTICATION_STRING + Environment.MachineName + "," + KEY_COMPUTER;
//active directory entry point for above connection string
objAD = new DirectoryEntry(strConn);

where strConn is connection string for native (NT) protocol and objAD is Active Directory entry point.

We will add new user using "Invoke" method which access special windows API functions:

objNewUser = objAD.Children.Add(USER_NAME, "user");
objNewUser.Invoke("SetPassword",
new object[] {USER_PWD});
objNewUser.Invoke("Put",
new object
[] {"Description", USER_DESCR});
objNewUser.CommitChanges();

After adding new user in Active Directory we can put this user in a properly group:

//finding group
objGrp = objAD.Children.Find(USER_GROUP, "group");
//adding new user to group
if
(objGrp.Name != "")
{
objGrp.Invoke("Add",
new object
[] {objNewUser.Path.ToString()});
}
//endif

Up Next
    Ebook Download
    View all
    Learn
    View all