UserPrincipals.GetAuthorizationGroups An error (1301) occurred while enumerating the groups. After upgrading to Server 2012 Domain Controller

后端 未结 10 1302
陌清茗
陌清茗 2020-12-29 21:17

Research:

Similar Issue with workaround, but not actual solution to existing problem

Similar issue pointing to Microsoft End Point update as

10条回答
  •  感动是毒
    2020-12-29 21:57

    I experienced error code 1301 with UserPrincipal.GetAuthorizationGroups while using a brand new virtual development domain which contained 2 workstations and 50 users/groups (many of which are the built in ones). We were running Windows Server 2012 R2 Essentials with two Windows 8.1 Enterprise workstations joined to the domain.

    I was able to recursively obtain a list of a user's group membership using the following code:

    class ADGroupSearch
    {
        List groupNames;
    
        public ADGroupSearch()
        {
            this.groupNames = new List();
        }
    
        public List GetGroups()
        {
            return this.groupNames;
        }
    
        public void AddGroupName(String groupName)
        {
            this.groupNames.Add(groupName);
        }
    
        public List GetListOfGroupsRecursively(String samAcctName)
        {
            PrincipalContext ctx = new PrincipalContext(ContextType.Domain, System.Environment.UserDomainName);
            Principal principal = Principal.FindByIdentity(ctx, IdentityType.SamAccountName, samAcctName);
            if (principal == null)
            {
                return GetGroups();
            }
            else
            {
                PrincipalSearchResult searchResults = principal.GetGroups();
    
                if (searchResults != null)
                {
                    foreach (GroupPrincipal sr in searchResults)
                    {
                        if (!this.groupNames.Contains(sr.Name))
                        {
                            AddGroupName(sr.Name);
                        }
                        Principal p = Principal.FindByIdentity(ctx, IdentityType.SamAccountName, sr.SamAccountName);
    
                        try
                        {
                            GetMembersForGroup(p);
                        }
                        catch (Exception ex)
                        {
                            //ignore errors and continue
                        }
                    }
    
                }
                return GetGroups();
            }
    
        }
    
    
    
        private void GetMembersForGroup(Principal group)
        {
            if (group != null && typeof(GroupPrincipal) == group.GetType())
            {
                GetListOfGroupsRecursively(group.SamAccountName);
            } 
        }
    
        private bool IsGroup(Principal principal)
        {
            return principal.StructuralObjectClass.ToLower().Equals("group");
        }
    }
    

提交回复
热议问题