Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Collections;
- using System.DirectoryServices;
- using System.Configuration;
- using System.Text.RegularExpressions;
- namespace FL.ActiveDirectory
- {
- public class GroupMembers
- {
- /// <summary>
- /// searchedGroups will contain all groups already searched, in order to
- /// prevent endless loops when there are circular structured in the groups.
- /// </summary>
- private static Hashtable searchedGroups = null;
- private static GroupMembers instance;
- private static DirectoryEntry ROOT;
- private static String LDAP_USERNAME;
- private static String LDAP_PASSWORD;
- public GroupMembers(){
- LDAP_USERNAME = @ConfigurationManager.AppSettings["LDAP_USERNAME"];
- LDAP_PASSWORD = @ConfigurationManager.AppSettings["LDAP_PASSWORD"];
- ROOT = new DirectoryEntry(ConfigurationManager.AppSettings["LDAP_PATH"].ToString(),
- LDAP_USERNAME, LDAP_PASSWORD);
- }
- /// <summary>
- /// Creates a single instance of this class.
- /// </summary>
- /// <returns>Single class instance.</returns>
- public static GroupMembers GetInstance()
- {
- lock (typeof(GroupMembers))
- {
- if (instance == null)
- {
- instance = new GroupMembers();
- }
- return instance;
- }
- }
- /// <summary>
- /// This method will return all users in the group passed in as a parameter
- /// the names returned are the SAM Account Name of the users.
- /// The function will recursively search all nested groups.
- /// Remark: if there are multiple groups with the same name, this function will just
- /// use the first one it finds.
- /// </summary>
- /// <param name="strGroupName">Name of the group, which the users should be retrieved from</param>
- /// <returns>ArrayList containing the SAM Account Names of all users in this group and any nested groups</returns>
- public ArrayList GetNestedGroupsUsers(string strGroupName)
- {
- ArrayList groupMembers = new ArrayList();
- searchedGroups = new Hashtable();
- DirectorySearcher search = new DirectorySearcher(ROOT);
- // find group
- //
- search.Filter = String.Format("(&(objectCategory=group)(cn={0}))", strGroupName);
- search.PropertiesToLoad.Add("distinguishedName");
- SearchResult sru = null;
- DirectoryEntry group;
- try
- {
- sru = search.FindOne();
- }
- catch (Exception ex)
- {
- throw ex;
- }
- group = sru.GetDirectoryEntry();
- groupMembers = getUsersInGroup(group.Properties["distinguishedName"].Value.ToString());
- return groupMembers;
- }
- /// <summary>
- /// getUsersInGroup will return all users in the group passed in as a parameter
- /// the names returned are the SAM Account Name of the users.
- /// The function will recursively search all nested groups.
- /// </summary>
- /// <param name="strGroupDN">DN of the group, which the users should be retrieved from</param>
- /// <returns>ArrayList containing the SAM Account Names of all users in this group and any nested groups</returns>
- private ArrayList getUsersInGroup(string strGroupDN)
- {
- ArrayList groupMembers = new ArrayList();
- searchedGroups.Add(strGroupDN, strGroupDN);
- // find all users in this group
- DirectorySearcher ds = new DirectorySearcher("LDAP://DC=company,DC=com");
- ds.Filter = String.Format("(&(memberOf={0})(objectClass=person))", strGroupDN);
- ds.PropertiesToLoad.Add("samaccountname");
- try
- {
- foreach (SearchResult sr in ds.FindAll())
- {
- groupMembers.Add(sr.Properties["samaccountname"][0].ToString());
- }
- }
- catch
- {
- //ignore if any properties found in AD
- }
- // get nested groups
- ArrayList al = getNestedGroups(strGroupDN);
- foreach (object g in al)
- {
- // only if we haven't searched this group before - avoid endless loops
- //
- if (!searchedGroups.ContainsKey(g))
- {
- // get members in nested group
- ArrayList ml = getUsersInGroup((string)g);
- // add them to result list
- foreach (object s in ml)
- {
- groupMembers.Add((string)s);
- }
- }
- }
- return groupMembers;
- }
- /// <summary>
- /// getNestedGroups will return an array with the DNs of all groups contained
- /// in the group that was passed in as a parameter
- /// </summary>
- /// <param name="strGroupDN">DN of the group, which the nested groups should be retrieved from</param>
- /// <returns>ArrayList containing the DNs of each group contained in the group passed in as a parameter</returns>
- private ArrayList getNestedGroups(string strGroupDN)
- {
- ArrayList groupMembers = new ArrayList();
- // find all nested groups in this group
- DirectorySearcher ds = new DirectorySearcher("LDAP://DC=company,DC=com");
- ds.Filter = String.Format("(&(memberOf={0})(objectClass=group))", strGroupDN);
- ds.PropertiesToLoad.Add("distinguishedName");
- foreach (SearchResult sr in ds.FindAll())
- {
- groupMembers.Add(sr.Properties["distinguishedName"][0].ToString());
- }
- return groupMembers;
- }
- public bool FindUserInGroup( string windowsLoginID, string groupName ){
- bool foundUser = false;
- if (Regex.IsMatch(windowsLoginID, @"[\\]"))
- {
- windowsLoginID = windowsLoginID.Substring(windowsLoginID.IndexOf(@"\") + 1);
- }
- ArrayList groupMembers = GetNestedGroupsUsers(groupName);
- if (groupMembers.Contains(windowsLoginID))
- {
- foundUser = true;
- }
- return foundUser;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement