主体已经存在LDAP C#

问题描述 投票:0回答:1

我正在尝试使用MVC C#LDAP更新Active Directory上的用户,如果用户已经在组中,我首先将其从组中删除,然后使用Principal上下文再次添加该用户。我试图在我的删除循环结束时提交更改,但这无济于事

这是我的代码删除代码:

PropertyValueCollection groups = result.GetDirectoryEntry().Properties["memberOf"];
                if (groups != null)
                {
                    for (int i = 0; i < groups.Count; i++)
                    {
                        string groupDn = (string)groups[i];

                        DirectoryEntry group = new DirectoryEntry("LDAP://" + groupDn, null, null);
                        if (group != null)
                        {
                            group.Invoke("Remove", new object[] { result.Path });
                        }
                        group.CommitChanges();
                    }
                }

将用户添加到组的代码

    //DirectoryEntry dirEntry = new DirectoryEntry(groupDirectoryEntry);
                using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, AD_Address))// "10.125.153.30"))
                {
                    foreach (var rights in accessGroupList)
                    {
                        GroupPrincipal group = GroupPrincipal.FindByIdentity(pc, rights);
                        group.Members.Add(pc, IdentityType.UserPrincipalName, model.validPersalNumber); //this is where is failes
                        group.Save();
                    }
                }
c# asp.net-mvc active-directory ldap-query principalcontext
1个回答
0
投票

提交更改后删除组代码,我必须关闭组

 PropertyValueCollection groups = result.GetDirectoryEntry().Properties["memberOf"];
                if (groups != null)
                {
                    for (int i = 0; i < groups.Count; i++)
                    {
                        string groupDn = (string)groups[i];

                        DirectoryEntry group = new DirectoryEntry("LDAP://" + groupDn, null, null);
                        if (group != null)
                        {
                            group.Invoke("Remove", new object[] { result.Path });
                        }
                        group.CommitChanges();
                        group.Close();
                    }
© www.soinside.com 2019 - 2024. All rights reserved.