public static void RefreshUser(CustomUser UserForChange) { using (DirectorySearcher dsSearcher = new DirectorySearcher()) { dsSearcher.Filter = InputLDAP.SearchFilter(UserForChange.SAMAccountName); SearchResult result = dsSearcher.FindOne(); DirectoryEntry de = result.GetDirectoryEntry(); HasUserInfo ifUser = new DeUser(de); CustomUser NewUser = new CustomUser(ifUser); UserForChange.Copy(NewUser); } }
public static bool ChangeCn(CustomUser ChangesHolder) { string OUpath = ""; Regex reg = new Regex(@"(=((\w+(\s)?)+(\w+)?))"); string NewDistName = reg.Replace(ChangesHolder.DistName, "=" + ChangesHolder.Cn, 1); try { using (DirectorySearcher dsSearcher = new DirectorySearcher()) { dsSearcher.Filter = InputLDAP.SearchFilter(ChangesHolder.SAMAccountName); SearchResult result = dsSearcher.FindOne(); using (DirectoryEntry user = new DirectoryEntry(result.Path)) { OUpath = user.Parent.GetProperty("distinguishedName"); } } using (var companyOU = new DirectoryEntry("LDAP://" + OUpath)) { companyOU.Invoke("MoveHere", "LDAP://" + ChangesHolder.DistName, NewDistName); } return(true); } catch (Exception e) { RefreshUser(ChangesHolder); if (!ChangesHolder.DistName.Equals(NewDistName, StringComparison.OrdinalIgnoreCase)) { MessageBox.Show("Не удалось изменить cn пользователя методом MoveHere" + e.Message, "Ошибка загрузки из AD", MessageBoxButtons.OK, MessageBoxIcon.Hand); return(false); } return(true); } }