【问题标题】:Find local groups that a domain user belongs to?查找域用户所属的本地组?
【发布时间】:2012-06-28 07:38:03
【问题描述】:

我正在使用 System.DirectoryServices.AccountManagement 命名空间来查找域用户及其相应的 AD 安全组。这很好用。

我还使用该命名空间来查询远程服务器上的本地安全组。我能够找到一个安全组,然后列出该组的用户没有问题。

我遇到的问题是显示 DOMAIN 用户所属的 LOCAL 组:

PrincipalContext localmachine = new PrincipalContext(ContextType.Machine, "ServerName");
PrincipalContext domain = new PrincipalContext(ContextType.Domain);

// find the user using the domain context (Works fine)
UserPrincipal user = UserPrincipal.FindByIdentity(domain, userName);

// if found - grab its groups
if (user != null)
{
    // The get groups method is the only method that would accept a new context
    PrincipalSearchResult<Principal> groups = user.GetGroups(localMachine);

    // no groups are returned .... removed rest of code
}

我正在尝试使用 GetGroups 方法传入 localMachine PrincipalContext 但没有返回任何组。

用户仅存在于域 AD 中。 localMachine 的本地用户中没有此用户的条目。域用户被添加到本地安全组。

有什么想法吗?我希望能够提取该域用户所属的所有本地组的列表,然后查看该列表中是否存在某个组。现在唯一可行的选择是搜索系统上的某些组,并查看域用户是否属于该组。

【问题讨论】:

  • 这里有类似的问题 - 希望有一些用处 - stackoverflow.com/questions/4809460/…
  • 我试过这样做。 FindByIdentity 方法在机器上查找用户时返回 null。我认为这是因为没有创建实际的本地用户。它是域用户。我已经尝试在有和没有域前缀的情况下传递用户名。
  • 挖掘了一些旧代码,我发现我完全按照你描述的方式完成了它;枚举本地机器组(通过 DirectorySearcher),然后枚举这些以查看是否可以找到用户所属的组。我们的层次结构也很浅。抱歉,无法提供更多帮助。
  • 您确定用户是本地组的成员,还是用户是本地组成员的域组成员。因此,您必须找到用户所属的所有域组,并找到每个域组所属的所有本地组。编辑:实际上,我对本地组中的域用户进行了测试,并得到了与您相同的结果-即。没有结果
  • 请参考此堆栈链接。stackoverflow.com/questions/12453804/…

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


【解决方案1】:

以下代码将返回域用户所属的本地组:

        PrincipalContext domain = new PrincipalContext(ContextType.Domain);
        UserPrincipal user = UserPrincipal.FindByIdentity(domain, userName);
        foreach (GroupPrincipal group in user.GetAuthorizationGroups())
        {
            if (group.Context.ConnectedServer == serverName)
                Console.Out.WriteLine("{0}\\{1}", group.Context.Name, group.SamAccountName);
        }

【讨论】:

  • 我本地机器上的这段代码只返回一个连接的服务器,大约 30 个组。我希望 GetAuthorizationGroups() 方法仅从 UserPrincipal 对象中指定的 Context 返回组。该方法是否也查询运行代码的机器?
【解决方案2】:

我知道我的回答迟了,但这对我有用(在我尝试了各种排列之后):

private static IList<string> GetUserLocalGroups(string userAccountName, string computerName, string domainName)
{
  List<string> groups = new List<string>();

  // We have to deal with a local computer
  DirectoryEntry root = new DirectoryEntry(String.Format("WinNT://{0},Computer", computerName), null, null, AuthenticationTypes.Secure);


  foreach (DirectoryEntry groupDirectoryEntry in root.Children)
  {
    if (groupDirectoryEntry.SchemaClassName != "Group")
      continue;

    string groupName = groupDirectoryEntry.Name;
    Console.WriteLine("Checking: {0}", groupName);
    if (IsUserMemberOfGroup(groupDirectoryEntry, String.Format("WinNT://{0}/{1}", domainName, userAccountName)))
    {
      groups.Add(groupName);
    }
  }

  return groups;
}

private static bool IsUserMemberOfGroup(DirectoryEntry group, string userPath)
{
  return (bool)group.Invoke(
      "IsMember",
      new object[] { userPath }
      );
}

调用是这样的:

GetUserLocalGroups("samaccountname", "computerName.yourdomain", "yourdomain");

【讨论】:

    猜你喜欢
    • 2011-04-10
    • 1970-01-01
    • 2023-03-16
    • 2014-07-18
    • 2017-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多