private void button2_Click(object sender, EventArgs e) { ADMethodsAccountManagement ADcon = new ADMethodsAccountManagement(); PasswordGenerator pass = new PasswordGenerator(); for (int i = 0; i < checkedListBox1.SelectedItems.Count; i++) { DataTable dt = m_ITOSQLCommand.ExecuteSQLCommand("Select login, Name,SecondName,LastName,UserID, dbo.GetEmailByID(emailID) from staff where login='******'"); if (dt.Rows.Count > 0) { //создаем пользователя в AD string password = pass.GeneratePassword(5); ADcon.CreateNewUser("CN=Users,DC=vniiaes-asutp,DC=lan", dt.Rows[0][0].ToString(), password, dt.Rows[0][1].ToString(), dt.Rows[0][3].ToString()); string UserID = dt.Rows[0][4].ToString(); string login = dt.Rows[0][0].ToString(); UserPrincipal up = ADcon.GetUser(dt.Rows[0][0].ToString()); up.DisplayName = dt.Rows[0][3].ToString() + " " + dt.Rows[0][1].ToString() + " " + dt.Rows[0][2].ToString(); up.MiddleName = dt.Rows[0][2].ToString(); up.PasswordNeverExpires = true; up.EmailAddress = dt.Rows[0][5].ToString(); up.Save(); //TODO Переделать добавление в группы по умолчанию // ADcon.AddUserToGroup(dt.Rows[0][0].ToString(),"1С-Битрикс - Сотрудники") ADcon.AddUserToGroup(dt.Rows[0][0].ToString(), "grpPermLocalUsers"); //Сохраняем пароль if (m_ITOSQLCommand.ExecuteSQLNotQuery(string.Format("insert into stfPasswords (UserID,Type,Password,LastChangeID) values ('{0}','1','{1}','{2}')", UserID, password, m_ProgramUserIdx.ToString())) != 1) MessageBox.Show("Innsert Pass " + pass + " to User " + login + " fails"); } //checkedListBox1.SelectedItems[i] } }
private void btnADSave_Click(object sender, EventArgs e) { ADMethodsAccountManagement ADcon = new ADMethodsAccountManagement(); //проверяем, если такой пользователь уже есть if (ADcon.IsUserExisiting(tbLogin.Text)) { MessageBox.Show("Такой пользователь уже есть"); return; } string password= null; PasswordGenerator pass = new PasswordGenerator(); password = pass.GeneratePassword(5); //пароль для AD UserPrincipal oUserPrincipal = ADcon.CreateNewUser("OU=RASU,DC=vniiaes-asutp,DC=lan", tbLogin.Text, password, txbName.Text, txbLastName.Text); oUserPrincipal.DisplayName = txbLastName.Text + " " + txbName.Text + " " + txbSecondName.Text; oUserPrincipal.MiddleName = txbSecondName.Text; //oUserPrincipal.PasswordNeverExpires = true; oUserPrincipal.EmailAddress = lblMainEmail.Text; //Добавляем пользователя в группы по умолчанию GroupPrincipal oGroupPrincipal = ADcon.GetGroup("OU=Share Permission,DC=vniiaes-asutp,DC=lan", "grpPermLocalUsers"); if (oGroupPrincipal != null) { oGroupPrincipal.Members.Add(oUserPrincipal); oGroupPrincipal.Save(); } oGroupPrincipal = ADcon.GetGroup("OU=Bitrix, OU=Share Permission,DC=vniiaes-asutp,DC=lan", "1С-Битрикс - Сотрудники"); if (oGroupPrincipal != null) { oGroupPrincipal.Members.Add(oUserPrincipal); oGroupPrincipal.Save(); } oGroupPrincipal = ADcon.GetGroup("OU=Service Permission,DC=vniiaes-asutp,DC=lan", "Wi-Fi Users"); if (oGroupPrincipal != null) { oGroupPrincipal.Members.Add(oUserPrincipal); oGroupPrincipal.Save(); } oUserPrincipal.Save(); try { m_ITOSQLCommand.ExecuteSQLNotQuery(string.Format("update staff set Login='******' where UserID='{1}'", tbLogin.Text, m_SelectedUserIdx.ToString())); } catch (SystemException ex) { } if (password != null) { try { m_ITOSQLCommand.ExecuteSQLNotQuery(string.Format("insert into stfPasswords (Password, Type,LastChangerID,UserID) values ('{0}','{1}','{2}','{3}')", password, (int)ePasswordTypes.AD, m_ProgramUserIdx.ToString(), m_SelectedUserIdx.ToString())); } catch (SystemException ex) { } } }