【问题标题】:get local groups and not the primary groups for a domain user获取本地组,而不是域用户的主要组
【发布时间】:2025-12-12 22:10:02
【问题描述】:

我有一个代码来获取用户所属的组。

try 
        {
            DirectoryEntry adRoot = new DirectoryEntry(string.Format("WinNT://{0}", Environment.UserDomainName));

            DirectoryEntry user = adRoot.Children.Find(completeUserName, "User");                
            object obGroups = user.Invoke("Groups");
            foreach (object ob in (IEnumerable)obGroups)
            {
                // Create object for each group.
                DirectoryEntry obGpEntry = new DirectoryEntry(ob);
                listOfMyWindowsGroups.Add(obGpEntry.Name);
            }
        return true;
        }
        catch (Exception ex)
        {
            new GUIUtility().LogMessageToFile("Error in getting User MachineGroups = " + ex);
            return false;
        }

当我必须找到本地用户的组时,上面的代码可以正常工作,但是

对于域用户,它返回一个值“域用户”,这有点奇怪,因为它是 2 个本地组的一部分。

请帮忙解开这个谜团。谢谢

研究

我做了一些发现,发现我正在返回域用户的主要组

称为“域用户”组

但我真正想要的是域用户所属的本地计算机组......我无法理解......任何建议

另一个使用 LDAP 的代码

        string domain = Environment.UserDomainName;
        DirectoryEntry DE = new DirectoryEntry("LDAP://" + domain, null, null, AuthenticationTypes.Secure);
        DirectorySearcher search = new DirectorySearcher();

        search.SearchRoot = DE;         
        search.Filter = "(SAMAccountName=" + completeUserName + ")";  //Searches active directory for the login name

        search.PropertiesToLoad.Add("displayName");  // Once found, get a list of Groups

        try
        {
            SearchResult result = search.FindOne(); // Grab the records and assign them to result
            if (result != null)
            {
                DirectoryEntry theUser = result.GetDirectoryEntry();
                theUser.RefreshCache(new string[] { "tokenGroups" });
                foreach (byte[] resultBytes in theUser.Properties["tokenGroups"])
                {
                    System.Security.Principal.SecurityIdentifier mySID = new System.Security.Principal.SecurityIdentifier(resultBytes, 0);

                    DirectorySearcher sidSearcher = new DirectorySearcher();

                    sidSearcher.SearchRoot = DE;
                    sidSearcher.Filter = "(objectSid=" + mySID.Value + ")";
                    sidSearcher.PropertiesToLoad.Add("distinguishedName");

                    SearchResult sidResult = sidSearcher.FindOne();

                    if (sidResult != null)
                    {
                        listOfMyWindowsGroups.Add((string)sidResult.Properties["distinguishedName"][0]);
                    }
                }
            }
            else
            {
                new GUIUtility().LogMessageToFile("no user found");

            }
            return true;
        }

        catch (Exception ex)
        {

            new GUIUtility().LogMessageToFile("Error obtaining group names: " + ex.Message + " Please contact your administrator."); // If an error occurs report it to the user.
            return false;
        }

这也有效,但我得到相同的结果 "Domain Users" 。请有人告诉我如何获取本地机器组...????

【问题讨论】:

  • @user175084 我的回答有用吗?有什么意见吗?

标签: c# asp.net active-directory dns


【解决方案1】:

如果您使用的是 .NET 3.5,则可以使用System.DirectoryService.AccountManagement 进行所有用户和组管理。特别是,UserPrincipal.GetAuthorizationGroups 正是您正在寻找的。它检索特定用户的本地组和机器组。如果该组是本地组,则 GroupPrincipal.Context.Name 显示该组来自的计算机名称。如果组是域组,则 GroupPrincipal.Context.Domain 显示该组来自的域名。

PrincipalContext context = new PrincipalContext(ContextType.Domain, "yourdomain.com");
UserPrincipal userPrincipal = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, "youruser"); 

foreach (GroupPrincipal group in userPrincipal.GetAuthorizationGroups())
{
    Console.Out.WriteLine("{0}\\{1}", group.Context.Name, group.SamAccountName);
}

【讨论】:

    【解决方案2】:

    我会说问题是您的搜索是在域中开始的。您想将搜索的位置更改为本地计算机。

    这样的事情就可以了;

    DirectoryEntry AD = new DirectoryEntry("WinNT://" + Environment.MachineName + ",computer");
    

    【讨论】:

    • 如果我将其更改为它只会寻找计算机中的用户而不是域用户..尝试过这个..谢谢