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

后端 未结 10 1283
陌清茗
陌清茗 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

    We experienced this issue when our infrastructure team brought a 2012 Domain Controller online. We also had pre-2012 DCs in place and so we experienced the issue intermittently. We came up with a fix which I wanted to share - it has 2 parts.

    First of all, install the hotfix mentioned by Gary Hill. This will resolve the following issue:

    An error (1301) occurred while enumerating the groups. The group's SID could not be resolved.

    We thought we were home free after installing this hotfix. However, after it was installed we got a different intermittent error. Certain groups that we were interrogating had a null sAMAccountName property. The actual property was populated in Active Directory but it was incorrectly being returned with a null value by the API. I presume this is a bug somewhere in the Active Directory API but I don't know any more than that.

    Fortunately we were able to work around the issue by switching to use the group Name property instead of the sAMAccountName property. This worked for us. I believe, that sAMAccountName is effectively deprecated and exists only for backwards compatibility reasons. That being the case it seemed a reasonable change to make.

    I enclose a cut down version of our GetRolesForUser code to demonstrate the change in place.

    using (var context = new PrincipalContext(ContextType.Domain, _domainName))
    {
        try
        {
            var p = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, username);
            if (p == null) throw new NullReferenceException(string.Format("UserPrincipal.FindByIdentity returned null for user: {0}, this can indicate a problem with one or more of the AD controllers", username));
    
            var groups = p.GetAuthorizationGroups();
            var domain = username.Substring(0, username.IndexOf(@"\", StringComparison.InvariantCultureIgnoreCase)).ToLower();
    
            foreach (GroupPrincipal group in groups)
            {
                if (!string.IsNullOrEmpty(group.Name))
                {
                    var domainGroup = domain + @"\" + group.Name.ToLower();
    
                    if (_groupsToUse.Any(x => x.Equals(domainGroup, StringComparison.InvariantCultureIgnoreCase)))
                    {
                        // Go through each application role defined and check if the AD domain group is part of it
                        foreach (string role in roleKeys)
                        {
                            string[] roleMembers = new [] { "role1", "role2" };
    
                            foreach (string member in roleMembers)
                            {
                                // Check if the domain group is part of the role
                                if (member.ToLower().Contains(domainGroup))
                                {
                                    // Cache the Application Role (NOT the AD role)
                                    results.Add(role);
                                }
                            }
                        }
                    }
                }
    
                group.Dispose();
            }
        }
        catch (Exception ex)
        {
            throw new ProviderException("Unable to query Active Directory.", ex);
        }
    }
    

    Hope that helps.

提交回复
热议问题