【发布时间】:2016-07-06 12:44:48
【问题描述】:
我需要向服务器上的本地组添加/删除对象(用户、组)。我这样做如下,它工作正常:
Principal adObject = Principal.FindByIdentity(domainContext, login);
GroupPrincipal groupPrincipal = GroupPrincipal.FindByIdentity(machineContext, IdentityType.Name, localGroupName);
groupPrincipal.Members.Add(adObject);
groupPrincipal.Save();
本地组包含一些孤立的 SID(Active Directory 用户或已被删除的组)的情况除外。
在这种情况下,我得到以下异常:
System.DirectoryServices.AccountManagement.PrincipalOperationException: An error (1332) occurred while enumerating the group membership. The member's SID could not be resolved.
当我尝试添加、删除和枚举本地组中的成员时,会出现此错误消息。在阅读以下解决方法的当前组成员时效果很好:
DirectoryEntry group = (DirectoryEntry)groupPrincipal.GetUnderlyingObject();
foreach (object member in (IEnumerable)group.Invoke("Members", null))
{
...
}
但是,将GroupPrincipal 转换为DirectoryEntry 并不能解决添加和删除新成员的问题。我尝试了以下三种方法,但都没有效果:
1) group.Invoke("Add", new object[] {@"WinNT://" + domain + "//" + login + ",user"});
2) group.Invoke("Add", new object[] { @"LDAP://" + adObject.DistinguishedName });
3) group.Properties["member"].Add(@"LDAP://" + adObject.DistinguishedName);
以上三种情况都给出相同的错误:
System.DirectoryServices.AccountManagement.PrincipalOperationException: An error (1332) occurred while enumerating the group membership. The member's SID could not be resolved.
at System.DirectoryServices.AccountManagement.SAMMembersSet.IsLocalMember(Byte[] sid)
at System.DirectoryServices.AccountManagement.SAMMembersSet.MoveNextLocal()
at System.DirectoryServices.AccountManagement.SAMMembersSet.MoveNext()
at System.DirectoryServices.AccountManagement.PrincipalCollectionEnumerator.MoveNext()
at System.DirectoryServices.AccountManagement.PrincipalCollection.ContainsEnumTest(Principal principal)
at System.DirectoryServices.AccountManagement.PrincipalCollection.Add(Principal principal)
我需要能够在不删除那些孤立的 SID 的情况下向组添加和删除用户。有人可以建议我解决这个问题吗?
【问题讨论】:
标签: c# exception active-directory adsi