如果计算机是组的成员,请询问ActiveDirectory

时间:2009-10-23 13:58:18

标签: c# .net active-directory ldap

这应该很容易,但由于某种原因,它似乎并不是。我想询问AD当前机器是否是特定组的成员。直接会员资格很好。

集团仅包含8台PC,极不可能超过30台。

C#代码示例赞赏!

1 个答案:

答案 0 :(得分:5)

以下是使用System.DirectoryServices命名空间的示例方法:

public bool BelongsToGroup(string computerName, string groupName, string domain)
{
   PrincipalContext domainContext = new PrincipalContext(ContextType.Domain, domain);

   ComputerPrincipal computer = ComputerPrincipal.FindByIdentity(domainContext, computerName);

   foreach (Principal result in computer.GetGroups())
   {
      if (result.Name == groupName)
      {
         return true;
      }
   }

  return false;
}

所以你可以这样称呼它:

string computerName = Environment.MachineName;
string groupName = "Group Name";
string domainName = "Domain Name";
bool test = BelongsToGroup(computerName, groupName, domainName);
相关问题