public string CreateUserAccount(string ldapPath, User U) { string oGUID = string.Empty; try { string connectionPrefix = "LDAP:" + ldapPath; // LDAP://cn=Users,DC=company,DC=com DirectoryEntry dirEntry = new DirectoryEntry(connectionPrefix); DirectoryEntry newUser = dirEntry.Children.Add("CN=" + U.givenName + " " + U.sn, "user"); newUser.Properties["samAccountName"].Value = U.userName; newUser.Properties["displayName"].Value = U.sn + " " + U.givenName + " " + U.SecondName; if (!String.IsNullOrEmpty(U.givenName)) { newUser.Properties["givenName"].Value = U.givenName; } if (!String.IsNullOrEmpty(U.department)) { newUser.Properties["department"].Value = U.department; } if (!String.IsNullOrEmpty(U.company)) { newUser.Properties["company"].Value = U.company; } if (!String.IsNullOrEmpty(U.title)) { newUser.Properties["title"].Value = U.title; } newUser.Properties["sn"].Value = U.sn; if (!("+38 " == U.mobile)) { newUser.Properties["mobile"].Value = U.mobile; } if (!("+38 " == U.telephone)) { newUser.Properties["telephoneNumber"].Value = U.telephone; } if (!(U.DateOfBirth == "") || !(U.DateOfBirth == " . .")) { newUser.Properties["employeeNumber"].Value = U.DateOfBirth; } if (!String.IsNullOrEmpty(U.DepOne)) { newUser.Properties["extensionAttribute1"].Value = U.DepOne; } if (!String.IsNullOrEmpty(U.SubDep)) { newUser.Properties["extensionAttribute2"].Value = U.SubDep; } if (!String.IsNullOrEmpty(U.ShortName)) { newUser.Properties["extensionAttribute3"].Value = U.ShortName; } newUser.Properties["userPrincipalName"].Value = U.userPrincipalName; newUser.CommitChanges(); oGUID = newUser.Guid.ToString(); newUser.Invoke("SetPassword", new object[] { U.Password }); int val = (int)newUser.Properties["userAccountControl"].Value; newUser.Properties["userAccountControl"].Value = val & ~0x2; //ADS_UF_NORMAL_ACCOUNT; newUser.CommitChanges(); dirEntry.Close(); newUser.Close(); } catch (System.DirectoryServices.DirectoryServicesCOMException E) { //DoSomethingwith --> E.Message.ToString(); MessageBox.Show(E.Message.ToString()); } return oGUID; }
private void ButtonDo_Click(object sender, EventArgs e) { //User User = new User(); //User.company = "113tv"; //User.department = "IT"; //User.givenName = "John"; //User.Password = "******"; //User.mobile = "+38 044 444 44 44"; //User.sn = "Stainback"; //User.telephone = "+38 099 993 33 33"; //User.title = "Engeneer"; //User.userName = "******"; //User.DateOfBirth = "22.03.1984"; //User.DepOne = "IT charts"; //User.SubDep = "SubIT"; //User.ShortName = "Some short name"; //User.userPrincipalName = User.userName + "@113tv.com"; User User = new User(); if ((PasswordBox.Text != PasswordConfirmBox.Text) || String.IsNullOrEmpty(PasswordConfirmBox.Text)) { PasswordConfirmBox.Text = String.Empty; MessageBox.Show("Password is empty or fields do not match!"); return; } if (string.IsNullOrEmpty(LoginBox.Text)) { MessageBox.Show("Login field cannot be empty!"); return; } if (string.IsNullOrEmpty(SurnameBox.Text)) { MessageBox.Show("Surname field cannot be empty!"); return; } User.company = Domain.Text; User.department = DepartmentBox.Text; User.givenName = NameBox.Text; User.Password = PasswordBox.Text; User.mobile = MobileBox.Text; User.sn = SurnameBox.Text; User.telephone = WorkPhoneBox.Text; User.title = TitleBox.Text; User.userName = LoginBox.Text; User.DateOfBirth = DateTextBox1.Text; User.DepOne = Dep2Box.Text; User.SubDep = SubDepBox.Text; User.ShortName = ShortBox.Text; User.userPrincipalName = User.userName + "@" + Domain.Text; ConsoleBox.AppendText("New user created " + CreateUserAccount("//" + OUBox.Text, User)); //AddToGroup("cn="+User.userName+""+User.sn+",dc=113tv,dc=com", GroupBox.Text); }