【问题标题】:Find out if a group in AD is in Distribution group?查明 AD 中的组是否在通讯组中?
【发布时间】:2011-12-19 05:37:51
【问题描述】:

我正在使用带有 C# 的 ASP.net,但对 Active Directory 知之甚少。我的任务是按以下步骤编写程序:

给 ASP.net 应用程序一个用户的用户名。

应用程序应查询具有给定用户名的用户的所有组。

然后应用程序应在两个单独的列表中显示这些组,其中一个包含通讯组,另一个列表中包含其余组。

现在,查询所有组很容易。但是如何检查该组是否在通讯组中?

我没有得到更多信息。

任何属性或我可以检查的东西?

【问题讨论】:

    标签: asp.net active-directory distribution active-directory-group


    【解决方案1】:

    您可以从名为Groupe-Type(最后一行)的属性中检索此信息。

    (0x00000001) : Specifies a group that is created by the system.
    (0x00000002) : Specifies a group with global scope.
    (0x00000004) : Specifies a group with domain local scope.
    (0x00000008) : Specifies a group with universal scope.
    (0x00000010) : Specifies an APP_BASIC group for Windows Server Authorization Manager.
    (0x00000020) : Specifies an APP_QUERY group fir Windows Server Authorization Manager.
    (0x80000000) :Specifies a security group. If this flag is not set, then the group is a distribution group.
    

    您可以在this answerthis other one 底部找到检索用户所属组的不同方法。

    你可以找到here如何找回用户。

    【讨论】:

    • 我还不确定你的答案。因为我还没弄清楚那里发生了什么!!!但我不问你的意思是什么,因为我想弄清楚自己,因为现在我有时间了。不过非常感谢。
    【解决方案2】:

    由于您使用的是 .NET 3.5 及更高版本,因此您应该查看 System.DirectoryServices.AccountManagement (S.DS.AM) 命名空间。在此处阅读所有相关信息:

    基本上,您可以定义域上下文并在 AD 中轻松找到用户和/或组:

    // set up domain context
    PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
    
    // find a user
    UserPrincipal user = UserPrincipal.FindByIdentity(ctx, "SomeUserName");
    
    if(user != null)
    { 
       // get all roles for that user
       var roles = user.GetGroups();
    
       // set up two lists for each type of groups
       List<GroupPrincipal> securityGroups = new List<GroupPrincipal>();
       List<GroupPrincipal> distributionGroups = new List<GroupPrincipal>();
    
       // iterate over groups found
       foreach (Principal p in roles)
       {
           // cast to GroupPrincipal
           GroupPrincipal gp = (p as GroupPrincipal);
    
           if (gp != null)
           {
               // check whether it's a security group or a distribution group
               if (gp.IsSecurityGroup)
                  securityGroups.Add(gp);
               else
                  distributionGroups.Add(gp);
           }
        }
    }
    

    新的 S.DS.AM 让在 AD 中与用户和组一起玩变得非常容易!

    【讨论】:

    • 非常感谢。这似乎有效。它给出了两个列表,但管理层声称这两个列表是错误的!这意味着某些通讯组在安全组列表中。也许他们错了。无论如何非常感谢。顺便说一句,我在编译时遇到了这个奇怪的错误:Cannot implicitly convert type 'bool?' to 'bool'. An explicit conversion exists (are you missing a cast?)。转换为布尔时没关系。但是这个数据类型'bool'到底是什么? ???没听说过!
    • @PPGoodMan:这是一个可为空的布尔值,这意味着它可以为 NULL、true 或 false。
    【解决方案3】:

    此代码将检索您所有启用电子邮件的组,无论它是安全组还是通讯组。 (看到您对 marc_s 回答的评论,我猜这实际上是您的经理正在寻找的)。

    using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
    {
        Principal prototype = new GroupPrincipal(ctx);
        PrincipalSearcher searcher = new PrincipalSearcher(prototype);
        List<string> groupNames = new List<string>();
        PropertyValueCollection email;
    
        foreach (var gp in searcher.FindAll()) using (gp)
        {
            GroupPrincipal group = gp as GroupPrincipal;
    
            using (DirectoryEntry groupEntry = ((DirectoryEntry)group.GetUnderlyingObject())
            {
              email = groupEntry.Properties["mail"];
              if (email.Value != null)
              {
                groupNames.Add(group.Name);
              }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-08
      • 1970-01-01
      • 1970-01-01
      • 2023-02-10
      相关资源
      最近更新 更多