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

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

    I had the problem that if i am connected over VPN and use groups=UserPrincipal.GetGroups() then the Exception occures when iterating over the groups.

    If someone want to read all groups of a user there is following possibility (which is faster than using GetGroups())

    private IList GetUserGroupsLDAP(string samAccountName)
    {
        var groupList = new List();
        var domainConnection = new DirectoryEntry("LDAP://" + serverName, serverUser, serverUserPassword); // probably you don't need username and password
    
        var samSearcher = new DirectorySearcher();
        samSearcher.SearchRoot = domainConnection;
        samSearcher.Filter = "(samAccountName=" + samAccountName + ")";
    
        var samResult = samSearcher.FindOne();
        if (samResult != null)
        {
            var theUser = samResult.GetDirectoryEntry();
            theUser.RefreshCache(new string[] { "tokenGroups" });
    
            var sidSearcher = new DirectorySearcher();
            sidSearcher.SearchRoot = domainConnection;
            sidSearcher.PropertiesToLoad.Add("name");
            sidSearcher.Filter = CreateFilter(theUser);
    
            foreach (SearchResult result in sidSearcher.FindAll())
            {
                groupList.Add((string)result.Properties["name"][0]);
            }
        }
        return groupList;
    }
    
    private string CreateFilter(DirectoryEntry theUser)
    {
        string filter = "(|";
        foreach (byte[] resultBytes in theUser.Properties["tokenGroups"])
        {
            var SID = new SecurityIdentifier(resultBytes, 0);
            filter += "(objectSid=" + SID.Value + ")";
        }
        filter += ")";
        return filter;
    }
    

提交回复
热议问题