Ejemplo n.º 1
0
        /// <summary>

        /// This function will take a DL or Group name and return list of users

        /// </summary>

        /// <param name="groupName"></param>

        /// <returns></returns>
        public List <ADUserDetail> GetUsers()
        {
            List <ADUserDetail> userlist = new List <ADUserDetail>();
            DirectoryEntry      user     = null;

            try
            {
                DirectoryEntry    de = GetRoot();
                DirectorySearcher ds = new DirectorySearcher(de);
                ds.Filter = "(objectCategory=user)";
                SearchResultCollection res = ds.FindAll();
                if (res != null)
                {
                    foreach (SearchResult r in res)
                    {
                        user = r.GetDirectoryEntry();

                        ADUserDetail userobj = ADUserDetail.GetUser(user);
                        userlist.Add(userobj);
                        user.Close();
                    }
                }
                return(userlist);
            }
            catch (Exception ex)
            {
                Trace.TraceError(ex.Message);
                string err = "GetUser from AD user server,  error: " + ex.Message;
                //int Event_id = 1120;
                //EventLog.WriteEntry(InfoHubEventLog.LogSource, err, EventLogEntryType.Warning, Event_id, Category);
                return(userlist);
            }
            finally
            {
                if (user != null)
                {
                    user.Close();
                }
            }
        }
Ejemplo n.º 2
0
        public List <ADUserDetail> GetUserFromGroup(String groupName)
        {
            List <ADUserDetail> userlist = new List <ADUserDetail>();

            try
            {
                DirectoryEntry    de = GetRoot();
                DirectorySearcher directorySearch = new DirectorySearcher(de);
                directorySearch.Filter = "(&(objectClass=group)(SAMAccountName=" + groupName + "))";
                SearchResult results = directorySearch.FindOne();
                if (results != null)
                {
                    DirectoryEntry deGroup = new DirectoryEntry(results.Path, LDAPUser, LDAPPassword);
                    System.DirectoryServices.PropertyCollection pColl = deGroup.Properties;
                    int count = pColl["member"].Count;
                    for (int i = 0; i < count; i++)
                    {
                        string   respath      = results.Path;
                        string[] pathnavigate = respath.Split("CN".ToCharArray());
                        respath = pathnavigate[0];
                        string         objpath = pColl["member"][i].ToString();
                        string         path    = respath + objpath;
                        DirectoryEntry user    = new DirectoryEntry(path, LDAPUser, LDAPPassword);
                        ADUserDetail   userobj = ADUserDetail.GetUser(user);
                        userlist.Add(userobj);
                        user.Close();
                    }
                }
                return(userlist);
            }
            catch (Exception ex)
            {
                Trace.TraceError(ex.Message);
                string err      = ex.Message;
                int    Event_id = 1122;
                EventLog.WriteEntry(InfoHubEventLog.LogSource, err, EventLogEntryType.Warning, Event_id, Category);
                return(userlist);
            }
        }
Ejemplo n.º 3
0
        public List <ADUserDetail> GetUsersByFirstName(string fName)
        {
            //UserProfile user;
            List <ADUserDetail> userlist = new List <ADUserDetail>();
            string filter = "";

            m_DirectoryEntry = null;
            DirectoryEntry    de = GetRoot();
            DirectorySearcher directorySearch = new DirectorySearcher(de);

            directorySearch.Asynchronous = true;
            directorySearch.CacheResults = true;
            filter = string.Format("(givenName={0}*", fName);
            //            filter = "(&(objectClass=user)(objectCategory=person)(givenName="+fName+ "*))";

            directorySearch.Filter = filter;
            SearchResultCollection userCollection = directorySearch.FindAll();

            foreach (SearchResult users in userCollection)
            {
                DirectoryEntry userEntry = new DirectoryEntry(users.Path, LDAPUser, LDAPPassword);
                ADUserDetail   userInfo  = ADUserDetail.GetUser(userEntry);
                userlist.Add(userInfo);
            }
            directorySearch.Filter = "(&(objectClass=group)(SAMAccountName=" + fName + "*))";
            SearchResultCollection results = directorySearch.FindAll();

            if (results != null)
            {
                foreach (SearchResult r in results)
                {
                    DirectoryEntry deGroup = new DirectoryEntry(r.Path, LDAPUser, LDAPPassword);
                    ADUserDetail   agroup  = ADUserDetail.GetUser(deGroup);
                    userlist.Add(agroup);
                }
            }
            return(userlist);
        }
Ejemplo n.º 4
0
 public ADUserDetail GetUserByLoginName(String userName)
 {
     try
     {
         DirectoryEntry    de = GetRoot();
         DirectorySearcher directorySearch = new DirectorySearcher(de);
         directorySearch.Filter = "(&(objectClass=user)(SAMAccountName=" + userName + "))";
         SearchResult results = directorySearch.FindOne();
         if (results != null)
         {
             DirectoryEntry user = new DirectoryEntry(results.Path, LDAPUser, LDAPPassword);
             return(ADUserDetail.GetUser(user));
         }
         return(null);
     }
     catch (Exception ex)
     {
         Trace.TraceError(ex.Message);
         string err      = ex.Message;
         int    Event_id = 1119;
         EventLog.WriteEntry(InfoHubEventLog.LogSource, err, EventLogEntryType.Warning, Event_id, Category);
         return(null);
     }
 }