【问题标题】:Unable to add/remove objects to group due to orphaned SIDs: PrincipalOperationException: An error (1332) occurred由于孤立的 SID,无法向组添加/删除对象:PrincipalOperationException:发生错误 (1332)
【发布时间】: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


    【解决方案1】:

    看来我找到了解决问题的方法:

    DirectoryEntry group = (DirectoryEntry)groupPrincipal.GetUnderlyingObject();
    IADsGroup nativeGroup = (IADsGroup)group.NativeObject; // https://msdn.microsoft.com/en-us/library/aa706022(v=vs.85).aspx
    nativeGroup.Remove("LDAP://" + adObject.Sid.Value);
    //nativeGroup.Remove(String.Format("WinNT:////{0}//{1}", domain, ID));
    //nativeGroup.Remove(String.Format( "NTDS:////{0}//{1}", domain, ID));
    

    如果您将 DirectoryEntry 转换为本机对象并将其转换为 ActiveDs.IADsGroup - Add()Remove() 方法可以正常工作

    【讨论】:

      猜你喜欢
      • 2021-06-12
      • 2014-02-09
      • 2017-06-04
      • 2016-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-06
      相关资源
      最近更新 更多