Example #1
0
        public static List <string> GetNestedGroupMem(out string guser, bool onComputer = false, string customUser = null)
        {
            List <string> SIDList = new List <string>();

            string userDn = GetDN(onComputer, out string getdnuser, customUser);

            guser = getdnuser.ToUpper();

            if (!string.IsNullOrEmpty(userDn))
            {
                using (var userEntry = GetSingleEntry(userDn))
                {
                    //https://www.morgantechspace.com/2015/08/active-directory-tokengroups-vs-memberof.html
                    //Use RefreshCach to get the constructed attribute tokenGroups.
                    userEntry.RefreshCache(new string[] { "tokenGroups" });

                    foreach (byte[] sid in userEntry.Properties["tokenGroups"])
                    {
                        string groupSID = new SecurityIdentifier(sid, 0).ToString();

                        SIDList.Add(groupSID.ToUpper());
                    }
                }

                //NT AUTHORITY\Authenticated Users
                SIDList.Add("S-1-5-11");
                //NT AUTHORITY\This Organization
                SIDList.Add("S-1-5-15");
            }

            return(SIDList);
        }