private void LoadAllUser()
        {
            try
            {
                foreach (PrincipalContext AD in GetPrincipalContexts())
                {
                    UserPrincipal     u      = new UserPrincipal(AD);
                    PrincipalSearcher search = new PrincipalSearcher(u);
                    foreach (UserPrincipal result in search.FindAll())
                    {
                        if (result.Enabled == true && result.EmailAddress != null && result.DisplayName != null)
                        {
                            simpleADObject user = new simpleADObject(result.DisplayName, result.DistinguishedName, result.EmailAddress, AD.Container);
                            Users.Add(user);
                        }
                    }
                    search.Dispose();
                }
            }

            catch (Exception e)
            {
                showError(e);
            }
        }
 private void buttonAdd_Click(object sender, EventArgs e)
 {
     simpleADObject[] users = Users.Where(a => a.DisplayNameWithDomain.ToLowerInvariant() == comboBoxMember.Text.ToLowerInvariant()).ToArray();
     if (users.Length == 1)
     {
         simpleADObject user = users[0];
         if (currentGroups.Where(a => a.Container == user.Container).ToArray().Length > 0)
         {
             addUserToMemberList(user.DistinguishedName);
             buttonSave.Enabled = true;
         }
     }
     else
     {
         if (users.Length > 1)
         {
             MessageBox.Show("Benutzer nicht eindeutig gefunden", "Information", MessageBoxButtons.OK, MessageBoxIcon.Error);
         }
         else
         {
             MessageBox.Show("Benutzer nicht gefunden", "Information", MessageBoxButtons.OK, MessageBoxIcon.Error);
         }
     }
 }
        private void addUserToMemberList(string distinguishedName)
        {
            string       displayName;
            string       container = null;
            string       domain    = null;
            SearchResult user;

            if (distinguishedName.Contains("CN=ForeignSecurityPrincipals"))
            {
                string sid = distinguishedName.Split(',')[0].Split('=')[1];
                user = GetUserForSid(sid);
            }
            else
            {
                user = GetUserForDN(distinguishedName);
            }
            if (user == null)
            {
                return;
            }

            foreach (PrincipalContext pc in Domains.Values)
            {
                if (user.Path.Contains(pc.Container))
                {
                    domain = pc.Container.Replace(",DC=", ".").Replace("DC=", "");
                }
                if (distinguishedName.Contains(pc.Container))
                {
                    container = pc.Container;
                }
            }
            if (domain == null)
            {
                throw new NotImplementedException("Unbekannte Domain für " + user.Path);
            }
            try
            {
                displayName = user.Properties["displayName"][0].ToString();
            }
            catch
            {
                ResultPropertyValueCollection t = user.Properties["name"];
                if (t.Count >= 1)
                {
                    displayName = user.Properties["name"][0].ToString();
                }
                else
                {
                    MessageBox.Show("Beim Bearbeiten dieser Gruppe kann es zu Fehlern kommen, da nicht unterstüzte Objekte in dieser Gruppe enthalten sind. Wenden Sie sich ggf. an die Hotline.");
                    return;
                }
            }
            if (GroupMember.Find(a => a.DistinguishedName == distinguishedName) == null)
            {
                simpleADObject obj;
                ListViewItem   lvi = new ListViewItem(displayName);
                if (user.Properties.Contains("mail"))
                {
                    lvi.SubItems.Add(user.Properties["mail"][0].ToString().ToLowerInvariant());
                    obj = new simpleADObject(displayName, distinguishedName, user.Properties["mail"][0].ToString().ToLowerInvariant(), container);
                }
                else
                {
                    lvi.SubItems.Add("keine Email Adresse vorhanden");
                    obj = new simpleADObject(displayName, distinguishedName, null, container);
                }
                lvi.SubItems.Add(domain);
                listViewMember.Items.Add(lvi);
                GroupMember.Add(obj);
            }
            else
            {
                MessageBox.Show("Benutzer ist bereits Mitglied", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        private void FindMyGroups()
        {
            try
            {
                string dn  = FindMyDN();
                string sid = FindMySid();
                if (dn == null)
                {
                    return;
                }
                if (sid == null)
                {
                    return;
                }
                string localFilter = "";
                if (ManagedByMe.Count > 0)
                {
                    localFilter = "(|(managedBy=" + dn + ")";
                    foreach (String s in ManagedByMe.Values)
                    {
                        localFilter += "(managedBy=" + s + ")";
                    }
                    localFilter += ")";
                }
                else
                {
                    localFilter = "(managedBy=" + dn + ")";
                }
                DirectorySearcher search = GetLocalDirectorySearcher();
                search.Filter = "(&(objectClass=group)" + localFilter + ")";
                foreach (SearchResult result in search.FindAll())
                {
                    string         name    = result.Properties["name"][0].ToString();
                    string         groupdn = result.Properties["distinguishedName"][0].ToString();
                    simpleADObject group   = new simpleADObject(name, groupdn, "", getLocalSearchRoot());
                    Groups.Add(group);
                }



                foreach (DirectorySearcher remoteSearch in GetRemoteDirectorySearchers())
                {
                    string domain       = remoteSearch.SearchRoot.Path.ToString().Replace("LDAP://", "");
                    string remoteFilter = "(&(objectClass=group)(|(member=CN=" + sid + ",CN=ForeignSecurityPrincipals," + domain + ")";
                    foreach (IdentityReference myGroups in WindowsIdentity.GetCurrent().Groups)
                    {
                        remoteFilter += "(member=CN=" + myGroups.Value + ",CN=ForeignSecurityPrincipals," + domain + ")";
                    }
                    remoteFilter       += "))";
                    remoteSearch.Filter = remoteFilter;
                    localFilter         = "(|";
                    foreach (SearchResult result in remoteSearch.FindAll())
                    {
                        string groupdn = result.Properties["distinguishedName"][0].ToString();
                        localFilter += "(managedBy=" + groupdn + ")";
                    }
                    localFilter        += ")";
                    remoteSearch.Filter = localFilter;
                    foreach (SearchResult result in remoteSearch.FindAll())
                    {
                        string         name    = result.Properties["name"][0].ToString();
                        string         groupdn = result.Properties["distinguishedName"][0].ToString();
                        simpleADObject group   = new simpleADObject(name, groupdn, "", domain);
                        Groups.Add(group);
                    }
                }

                comboBoxGruppe.Items.Clear();
                Groups.Sort(delegate(simpleADObject c1, simpleADObject c2) { return(c1.DisplayName.CompareTo(c2.DisplayName)); });
                string        prevGroup    = "";
                List <string> groupDomains = new List <string>();
                DisplayToNameMap.Clear();

                if (Groups.Count > 0)
                {
                    var    maxLength = Groups.Max(a => a.DisplayName.Length);
                    string format    = "{0, -" + maxLength + "} ({1})";
                    foreach (simpleADObject group in Groups.ToArray())
                    {
                        if (group.DisplayName.ToLower() == prevGroup.ToLower())
                        {
                            groupDomains.Add(group.Domain);
                        }
                        else
                        {
                            if (prevGroup != "" && groupDomains.Count > 0)
                            {
                                string text = String.Format(format, prevGroup, String.Join(", ", groupDomains.ToArray()));
                                comboBoxGruppe.Items.Add(text);
                                DisplayToNameMap.Add(text, prevGroup);
                            }
                            prevGroup    = group.DisplayName;
                            groupDomains = new List <string>();
                            groupDomains.Add(group.Domain);
                        }
                    }
                    if (prevGroup != "" && groupDomains.Count > 0)
                    {
                        string text = String.Format(format, prevGroup, String.Join(", ", groupDomains.ToArray()));
                        comboBoxGruppe.Items.Add(text);
                        DisplayToNameMap.Add(text, prevGroup);
                    }
                    comboBoxGruppe.Items.Add("bitte wählen");
                    comboBoxGruppe.SelectedIndexChanged -= this.comboBoxGruppe_SelectedIndexChanged;
                    comboBoxGruppe.SelectedIndex         = comboBoxGruppe.Items.IndexOf("bitte wählen");
                    comboBoxGruppe.SelectedIndexChanged += this.comboBoxGruppe_SelectedIndexChanged;
                }
            }
            catch (Exception e)
            {
                showError(e);
            }
        }