Miguel Simes

Miguel Simes

  • NA
  • 1
  • 9.4k

c# Add User to group in active directory

Jun 17 2014 11:08 AM
Hi I have a problem, I can't add a user to a group,
please help me
 
this is my code:
 
 
private void button2_Click(object sender, EventArgs e)
{
Cursor.Current = Cursors.WaitCursor;
//Buscar numero de utilizadores disponiveis
try
{
con.Open();
DataTable dt3 = new DataTable();
DataSet ds3 = new DataSet();
ds3.Tables.Add(dt3);
string da3;
da3 = "SELECT cliUsersDisp FROM cliente WHERE cliCode LIKE ('" + textBox1.Text + "')";
SqlCommand com3 = new SqlCommand(da3, con);
SqlDataAdapter adapter3 = new SqlDataAdapter(da3, con);
adapter3.Fill(ds3);
SqlDataReader dr3 = com3.ExecuteReader();
while (dr3.Read())
{
textBox2.Text = dr3.GetValue(0).ToString();
}
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
//Reduzir utilizadores
if (textBox2.Text != "0")
{
try
{
con.Open();
DataTable dt2 = new DataTable();
DataSet ds2 = new DataSet();
ds2.Tables.Add(dt2);
string da2;
da2 = "UPDATE cliente SET cliUsersDisp = cliUsersDisp - 1 WHERE cliCode LIKE ('" + textBox1.Text + "')";
SqlCommand com2 = new SqlCommand(da2, con);
SqlDataAdapter adapter2 = new SqlDataAdapter(da2, con);
com2.ExecuteNonQuery();
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
}
else
{
MessageBox.Show("Exedeu o limite de utilizadores!");
return;
}
con.Open();
DataTable dt = new DataTable();
DataSet ds = new DataSet();
ds.Tables.Add(dt);
string da;
da = "SELECT cliFormato FROM cliente WHERE cliCode LIKE ('" + textBox1.Text + "')";
SqlCommand com = new SqlCommand(da, con);
SqlDataAdapter adapter = new SqlDataAdapter(da, con);
adapter.Fill(ds);
SqlDataReader dr = com.ExecuteReader();
while (dr.Read())
{
txttipo.Text = dr.GetValue(0).ToString();
}
con.Close();
//adicionar à Active Directory
try
{
//autenticação
DirectoryEntry dirEntry = new DirectoryEntry();
dirEntry.Path = "LDAP://" + this.TxtDomainController.Text;
dirEntry.Username = this.TxtAdminUser.Text;
dirEntry.Password = this.TxtAdminPassword.Text;
//configuração
DirectoryEntry ou1 = dirEntry.Children.Find("OU=" + txtitem.Text + "");
DirectoryEntry ou2 = ou1.Children.Find("OU=" + txttipo.Text + "");
DirectoryEntry ou3 = ou2.Children.Find("OU=" + textBox1.Text + "");
DirectoryEntry newUser = ou3.Children.Add("CN="+txtuser.Text+"", "user");
//propriadades
newUser.Properties["description"].Value = ""+txtdescrição.Text+"";
newUser.Properties["samAccountName"].Value = "" + txtuser.Text + "";
newUser.Properties["userPrincipalName"].Value = "" + txtuser.Text + "";
newUser.Properties["mail"].Value = "" + txtemail.Text + "";
///////////////
//commit
dirEntry.CommitChanges();
newUser.CommitChanges();
//Colocar password e ativar o utilizador
newUser.Invoke("SetPassword", new object[] { ""+txtpassword.Text+"" });
newUser.Properties["userAccountControl"].Value = 0x200;
newUser.CommitChanges();
DirectoryEntry group = ou3.Children.Find("CN="+textBox1.Text+"");
// Add the user to the group.
group.Properties["member"].Add("cn="+txtuser.Text+",ou="+txtitem.Text+",ou="+txtitem.Text+",ou"+textBox1.Text+",dc=estagio,dc=local");
// Commit the changes to the group.
group.CommitChanges();
//Fechar
dirEntry.Close();
newUser.Close();
MessageBox.Show("Adicionado com sucesso");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
Cursor.Current = Cursors.Default;
}