Advertisement
Guest User

Untitled

a guest
May 11th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.64 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Collections;
  6. using System.DirectoryServices;
  7. using System.Configuration;
  8. using System.Text.RegularExpressions;
  9.  
  10. namespace FL.ActiveDirectory
  11. {
  12.     public class GroupMembers
  13.     {
  14.  
  15.         /// <summary>
  16.         /// searchedGroups will contain all groups already searched, in order to
  17.         /// prevent endless loops when there are circular structured in the groups.
  18.         /// </summary>
  19.         private static Hashtable searchedGroups = null;
  20.         private static GroupMembers instance;
  21.         private static DirectoryEntry ROOT;
  22.         private static String LDAP_USERNAME;
  23.         private static String LDAP_PASSWORD;
  24.  
  25.         public GroupMembers(){
  26.             LDAP_USERNAME = @ConfigurationManager.AppSettings["LDAP_USERNAME"];
  27.             LDAP_PASSWORD = @ConfigurationManager.AppSettings["LDAP_PASSWORD"];
  28.             ROOT = new DirectoryEntry(ConfigurationManager.AppSettings["LDAP_PATH"].ToString(),
  29.                 LDAP_USERNAME, LDAP_PASSWORD);
  30.         }
  31.  
  32.  
  33.         /// <summary>
  34.         /// Creates a single instance of this class.
  35.         /// </summary>
  36.         /// <returns>Single class instance.</returns>
  37.         public static GroupMembers GetInstance()
  38.         {
  39.             lock (typeof(GroupMembers))
  40.             {
  41.                 if (instance == null)
  42.                 {
  43.                     instance = new GroupMembers();
  44.                 }
  45.                 return instance;
  46.             }
  47.         }
  48.  
  49.  
  50.         /// <summary>
  51.         /// This method will return all users in the group passed in as a parameter
  52.         /// the names returned are the SAM Account Name of the users.
  53.         /// The function will recursively search all nested groups.
  54.         /// Remark: if there are multiple groups with the same name, this function will just
  55.         /// use the first one it finds.
  56.         /// </summary>
  57.         /// <param name="strGroupName">Name of the group, which the users should be retrieved from</param>
  58.         /// <returns>ArrayList containing the SAM Account Names of all users in this group and any nested groups</returns>
  59.         public ArrayList GetNestedGroupsUsers(string strGroupName)
  60.         {
  61.             ArrayList groupMembers = new ArrayList();
  62.             searchedGroups = new Hashtable();
  63.             DirectorySearcher search = new DirectorySearcher(ROOT);
  64.             // find group
  65.             //
  66.             search.Filter = String.Format("(&(objectCategory=group)(cn={0}))", strGroupName);
  67.             search.PropertiesToLoad.Add("distinguishedName");
  68.             SearchResult sru = null;
  69.             DirectoryEntry group;
  70.  
  71.             try
  72.             {
  73.                 sru = search.FindOne();
  74.             }
  75.             catch (Exception ex)
  76.             {
  77.                 throw ex;
  78.             }
  79.             group = sru.GetDirectoryEntry();
  80.  
  81.             groupMembers = getUsersInGroup(group.Properties["distinguishedName"].Value.ToString());
  82.  
  83.             return groupMembers;
  84.         }
  85.  
  86.         /// <summary>
  87.         /// getUsersInGroup will return all users in the group passed in as a parameter
  88.         /// the names returned are the SAM Account Name of the users.
  89.         /// The function will recursively search all nested groups.
  90.         /// </summary>
  91.         /// <param name="strGroupDN">DN of the group, which the users should be retrieved from</param>
  92.         /// <returns>ArrayList containing the SAM Account Names of all users in this group and any nested groups</returns>
  93.         private ArrayList getUsersInGroup(string strGroupDN)
  94.         {
  95.             ArrayList groupMembers = new ArrayList();
  96.             searchedGroups.Add(strGroupDN, strGroupDN);
  97.  
  98.             // find all users in this group
  99.             DirectorySearcher ds = new DirectorySearcher("LDAP://DC=company,DC=com");
  100.             ds.Filter = String.Format("(&(memberOf={0})(objectClass=person))", strGroupDN);
  101.             ds.PropertiesToLoad.Add("samaccountname");
  102.  
  103.             try
  104.             {
  105.                 foreach (SearchResult sr in ds.FindAll())
  106.                 {
  107.                     groupMembers.Add(sr.Properties["samaccountname"][0].ToString());
  108.    
  109.                 }
  110.             }
  111.             catch
  112.             {
  113.                 //ignore if any properties found in AD
  114.             }
  115.  
  116.             // get nested groups
  117.             ArrayList al = getNestedGroups(strGroupDN);
  118.             foreach (object g in al)
  119.             {
  120.                 // only if we haven't searched this group before - avoid endless loops
  121.                 //
  122.                 if (!searchedGroups.ContainsKey(g))
  123.                 {
  124.                     // get members in nested group
  125.                     ArrayList ml = getUsersInGroup((string)g);
  126.                     // add them to result list
  127.                     foreach (object s in ml)
  128.                     {
  129.                         groupMembers.Add((string)s);
  130.                     }
  131.                 }
  132.             }
  133.                        
  134.             return groupMembers;
  135.         }
  136.  
  137.         /// <summary>
  138.         /// getNestedGroups will return an array with the DNs of all groups contained
  139.         /// in the group that was passed in as a parameter
  140.         /// </summary>
  141.         /// <param name="strGroupDN">DN of the group, which the nested groups should be retrieved from</param>
  142.         /// <returns>ArrayList containing the DNs of each group contained in the group passed in as a parameter</returns>
  143.         private ArrayList getNestedGroups(string strGroupDN)
  144.         {
  145.             ArrayList groupMembers = new ArrayList();
  146.  
  147.             // find all nested groups in this group
  148.             DirectorySearcher ds = new DirectorySearcher("LDAP://DC=company,DC=com");
  149.             ds.Filter = String.Format("(&(memberOf={0})(objectClass=group))", strGroupDN);
  150.  
  151.             ds.PropertiesToLoad.Add("distinguishedName");
  152.  
  153.             foreach (SearchResult sr in ds.FindAll())
  154.             {
  155.                 groupMembers.Add(sr.Properties["distinguishedName"][0].ToString());
  156.             }
  157.  
  158.             return groupMembers;
  159.         }
  160.  
  161.  
  162.         public bool FindUserInGroup( string windowsLoginID, string groupName ){
  163.  
  164.             bool foundUser = false;
  165.  
  166.             if (Regex.IsMatch(windowsLoginID, @"[\\]"))
  167.             {
  168.                 windowsLoginID = windowsLoginID.Substring(windowsLoginID.IndexOf(@"\") + 1);
  169.             }
  170.  
  171.  
  172.             ArrayList groupMembers = GetNestedGroupsUsers(groupName);
  173.             if (groupMembers.Contains(windowsLoginID))
  174.             {
  175.                 foundUser = true;
  176.             }
  177.             return foundUser;
  178.         }
  179.  
  180.     }
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement