2011-08-22 75 views
4

我已经写了一些代码来获取组和嵌套组的所有用户。我还想确保,如果组成员身份通过让第一组成为最后一组的成员而导致循环,则循环不会发生。使用C#枚举嵌套的AD用户组

我写的代码工作正常,但有点慢。

这是我第一次尝试做AD查找。

有人可以看一看,并告诉我,如果代码看起来好还是坏 - 编码(或更糟糕的),或者我已经走了错路?

using System; 
using System.Collections.Generic; 
using System.Text; 
using System.DirectoryServices; 
using System.IO; 

namespace Tester3 
{ 
    class Program3 
    { 
     public static List<string> appGroupList = new List<string>();   
     public static List<string> userList = new List<string>(); 
     public static List<string> groupList = new List<string>(); 
     public static List<string> groupChecked = new List<string>(); 

     static void Main(string[] args) 
     { 
      // Create Output File 
      StreamWriter outputfile = new StreamWriter("output.txt", false); 

      appGroupList.Add("GLB-SBCCitrixHelpdesk-DL"); 
      appGroupList.Add("SBC_UKBSAVIA001_PROD_ROL_Siebel"); 

      foreach (string appGroup in appGroupList) 
      { 
       string appGroupCN = GetCN(appGroup); 

       GetMembers(appGroupCN); 

       groupChecked.Clear(); 
      } 

      foreach (string item in userList) 
      { 
       Console.WriteLine(item); 
       outputfile.WriteLine(item); 
      } 

      outputfile.Flush(); 
      outputfile.Close(); 
      Console.ReadLine(); 
     } 

     private static string GetCN(string group) 
     { 
      string groupCN = string.Empty; 

      try 
      { 
       using (DirectorySearcher search = new DirectorySearcher()) 
       { 
        search.Filter = "(&(cn=" + group + ")(objectClass=group))"; 
        search.PropertiesToLoad.Add("CN"); 
        SearchResult result = search.FindOne(); 

        if (result != null) 
        { 
         groupCN = result.Properties["adsPath"][0].ToString(); 
         groupCN = groupCN.Replace("LDAP://", ""); 
        } 

        return groupCN; 
       } 
      } 
      catch (Exception) 
      { 
       return groupCN; 
      } 
     } 

     public static void GetMembers(string group) // get members using the groups full cn 
     { 
      // Check if group has already been checked 
      if (groupChecked.Contains(group)) 
      { 
       return; 
      } 

      // Add group to groupChecked list 
      groupChecked.Add(group); 

      try 
      { 
       // Connect to group object 
       using (DirectoryEntry groupObject = new DirectoryEntry("LDAP://" + group)) 
       { 
        // Get member of group object 
        PropertyValueCollection col = groupObject.Properties["member"] as PropertyValueCollection; 

        // Loop through each member 
        foreach (object member in col) 
        { 
         // Connect to member object 
         using (DirectoryEntry memberObject = new DirectoryEntry("LDAP://" + member)) 
         { 
          // Get class of member object 
          string memberClass = memberObject.Properties["objectClass"][1].ToString(); 
          string memberCN = memberObject.Properties["Name"][0].ToString(); 

          if (!groupChecked.Contains(member.ToString())) 
          { 
           if (memberClass.ToLower() == "group") 
           {          
            GetMembers(member.ToString()); 
           } 
           else 
           { 
            userList.Add(memberCN); 
           } 
          } 
          else 
          { 
           if (memberClass.ToLower() != "group") 
           { 
            userList.Add(memberCN); 
           } 
          } 
         } 
        } 
       } 
      } 
      catch (Exception) 
      { 
      } 
     } 
    } 
} 
+0

你可以看看[这个答案] [1]? [1]:http://stackoverflow.com/questions/6252819/find-recursive-group-membership-active-directory-using-c/6289205#6289205 – JPBlanc

回答

1

如果你在.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 a user's group memberships 
    foreach(Principal principal in me.GetGroups()) 
    { 
     GroupPrincipal gp = (principal as GroupPrincipal); 

     if(gp != null) 
     { 
      // do something with the group 
     } 
    } 
} 

的新的S.DS.AM使得与AD中的用户和群组玩转非常容易。致电.GetGroups()也会处理所有嵌套组员问题等问题 - 无需再处理这种麻烦!

+0

我没有看新的.Net 3.5代码,但我的最终应用程序将不得不在仅安装.net 2.0的服务器上工作。 – RickBowden

+0

@RickBowden:但.NET 3.5只是.NET 2.0的一个小“服务包” - 没有什么全新的...... –

+0

的确如此,但我在如此紧密(过度)管理的变更控制环境中工作升级这些东西是一场噩梦。 – RickBowden