DirectorySearcher filter query RRS feed

  • Question

  • User611134253 posted

    I am working in a application where i have to authenticate with the Active directory users. Here i would like to retrieve all the users in a particular GROUP. I was successful in retrieving the same by means of iterating member properties for the GROUP. But i would like to get all the users in a particular group using DirectorySearcher.Filter.
    For example.
    DirectorySearcher.Filter =  "(&(objectClass=user))(memberOf=CN=Administrators ,OU = <organization unit> , DC=<Name>,DC=Com))";
    foreach (SearchResult res in results)
     MessageBox.Show( DirectorySearcher.GetDirectoryEntry().Name)
    But the above code didnt work for me. Is it possible to retrieve the users of particular group using filters? Please suggest me some solutions.

    Thanks in advance

    Tuesday, July 21, 2009 10:00 AM

All replies

  • User-532251451 posted

    Hi Siva,

    I am actually trying to search user on AD and get his contact Info like his Display Name & Email Address based on his Shortname.

     public class InfiniaUserInfo
            public string ShortName { get; set; }
            public string DisplayName { get; set; }
            public string Email { get; set; }
    /// <summary>
            /// Returns UserInfo Object,
            /// Which contains User Display Name,Email Address
            /// </summary>
            /// <param name="_userName">User ShortName for Infinian, VPN External A/c Name for Vendors/Suppliers</param>
            /// <returns></returns>
            public static List<InfiniaUserInfo> GetCurrentUserInfo(string _userName)
                if (String.IsNullOrEmpty(_userName))
                    log.Error("UserName is null or Empty for GetCurrentUserInfo[_userName]" + _userName);
                    return null;                
                    string ldapqry = ConfigurationManager.AppSettings["ADConnctionstr"].ToString(); //@"LDAP://DC01.eng.stclocal";  
                    string qryFilterFormat = String.Format("(&(objectClass=user)(objectCategory=person)(sAMAccountName={0}))", _userName);
                    SearchResult result = null;
                    using (DirectoryEntry root = new DirectoryEntry(ldapqry))
                        using (DirectorySearcher searcher = new DirectorySearcher(root))
                            List<InfiniaUserInfo> userInfo = new List<InfiniaUserInfo>();
                            InfiniaUserInfo objuser = new InfiniaUserInfo(); 
                            searcher.Filter = qryFilterFormat;
                            SearchResultCollection results = searcher.FindAll();
                            result = (results.Count != 0) ? results[0] : null;
                            objuser.ShortName = (string) result.Properties["sAMAccountName"][0];
                            objuser.DisplayName = (string)result.Properties["displayname"][0];
                                objuser.Email = (string)result.Properties["mail"][0];  
                                objuser.Email = (string)result.Properties["sAMAccountName"][0];
                            return userInfo;
                catch (Exception ex)
                    log.Error("Error to getting Email Address for GetCurrentUserEmail [username]" + _userName + "Error Message: " + ex.Message.ToString());
                    return null;                
    protected void Session_Start(object sender, EventArgs e)
                List<InfiniaUserInfo> objUserInfo = new List<InfiniaUserInfo>(); 
                objUserInfo = InfiniaUtilities.GetCurrentUserInfo(InfiniaUtilities.GetCurrentUserShortName());
                Session["ShortName"] = objUserInfo[0].ShortName;
                Session["DisplayName"] = objUserInfo[0].DisplayName;
                Session["Email"] = objUserInfo[0].Email;   

    Tuesday, July 21, 2009 1:02 PM
  • User-60558687 posted

    Filter for groups

    something like

    DirectorySearcher ds = ...

    ds.Filter ="(&(objectClass=group)(cn=groupName))";

    Tuesday, July 21, 2009 1:10 PM