Add a user to a given active directory group

Add a user to a given active directory group

Note: To add or read properties from an Active Directory group you need to have special username and password like an Active Directory admin.

DirectoryEntry addGroup;
string folderPath = "LDAP://CN="+grpName+"OU=”+FolderName+”,OU=Groups,DC=aaaaa,DC=com";
addGroup = new DirectoryEntry(folderPath, "Spluser""password");

string usrId = “myUserID”;
string usrPath = "LDAP://CN="+usrId+”,OU=UserAccounts,DC=aaaaa,DC=com";
DirectoryEntry addUser;
addUser = new DirectoryEntry(usrPath);

if((chkGroupExistence(addGroup) == true)&&(chkUserExistence(addGroup, usrId) == false))      
{
  try
    {
      addGroup.Invoke("Add"new object[] { addUser.Path.ToString() });
    }
  catch (Exception)
    {
        LblSuccessMsg.Text = "User could not be added to the Group. ";
    }
 }

Leave your comments below.

No comments: