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

后端 未结 10 1204
陌清茗
陌清茗 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 22:08

    Facing the same problem enumerating authorization groups and the patches noted in the answer did not apply to our web server.

    Manually enumerating and ignoring the trouble causing groups is working well, however:

    private static bool UserIsMember(string usr, string grp)
    {
        usr = usr.ToLower();
        grp = grp.ToLower();
    
        using (var pc = new PrincipalContext(ContextType.Domain, "DOMAIN_NAME"))
        {
            using (var user = UserPrincipal.FindByIdentity(pc, usr))
            {
                var isMember = false;
                var authGroups = user?.GetAuthorizationGroups().GetEnumerator();
    
                while (authGroups?.MoveNext() ?? false)
                {
                    try
                    {
    
                        isMember = authGroups.Current.Name.ToLower().Contains(grp);
                        if (isMember) break;
                    }
                    catch
                    {
                        // ignored
                    }
                }
    
                authGroups?.Dispose();
                return isMember;
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-29 22:12

    we had a similar issue after upgrading the domain controller to 2012. Suddenly my call to user.GetAuthorizationGroups() started failing; I was getting the same exception you were (error 1301). So, I changed it to user.GetGroups(). That worked for a little while, then started failing intermittently on "bad username or password". My latest workaround appears to fix it, for the moment at least. Instead of calling either of those, after constructing the user object, I also construct a group object, one for each group I want to see if the user is a member of. ie, "user.IsMemberOf(group)". That seems to work.

    try
    {
    using (HostingEnvironment.Impersonate())
    {
        using (var principalContext = new PrincipalContext(ContextType.Domain, "MYDOMAIN"))
        {
            using (var user = UserPrincipal.FindByIdentity(principalContext, userName))
            {
                if (user == null)
                {
                    Log.Debug("UserPrincipal.FindByIdentity failed for userName = " + userName + ", thus not authorized!");
                    isAuthorized = false;
                }
    
                if (isAuthorized)
                {
                    firstName = user.GivenName;
                    lastName = user.Surname;
    
                    // so this code started failing:
    
                    // var groups = user.GetGroups();
                    // adGroups.AddRange(from @group in groups where 
                    // @group.Name.ToUpper().Contains("MYSEARCHSTRING") select @group.Name);
    
                    // so the following workaround, which calls, instead, 
                    // "user.IsMemberOf(group)", 
                    // appears to work (for now at least).  Will monitor for issues.
    
                    // test membership in SuperUsers
                    const string superUsersGroupName = "MyApp-SuperUsers";
                    using (var superUsers = GroupPrincipal.FindByIdentity(principalContext, superUsersGroupName))
                    {
                        if (superUsers != null && user.IsMemberOf(superUsers))
                            // add to the list of groups this user is a member of
                            // then do something with it later
                            adGroups.Add(superUsersGroupName);                                        
                    }
    
    0 讨论(0)
  • 2020-12-29 22:13

    I'm in an environment with multiple domain forests and trusts. I have pretty much this exact same code running on a web site form used to perform user security group lookups across the different domains.

    I get this exact error in one of the very large domains where group membership can include 50+ different groups. It works fine in other domains forests.

    In my research I found a thread that looks unrelated, but actually has the same stack trace. It is for a remote application running on SBS. The thread mentions that the error is caused by unresolvable SIDS in a group. I believe these would be what are known as "tombstoned" SIDS in active directory. See the thread here.

    The thread suggests that finding the tombstoned enteries and removing them from the groups solves the problem. Is it possible the error you are receiving is because SIDS are getting tombstoned every 12 hours by a separate unrelated process? Ultimately, I believe this is a bug in the framework, and that the method should not crash because of tombstoned/unresolvable SIDS.

    Good luck!

    0 讨论(0)
  • 2020-12-29 22:15

    I had same exception. If someone don't wanna used "LDAP", use this code. Cause I'm had nested groups, I'm used GetMembers(true) and it's little bit longer in time than GetMembers().

    https://stackoverflow.com/a/27548271/1857271

    or download fix from here: http://support.microsoft.com/kb/2830145

    0 讨论(0)
提交回复
热议问题