locked
Get all users from AD & display to Gridview RRS feed

  • Question

  • User-1001485597 posted

    Hi everyone,

     How do I get all users from AD & display it to gridview?  I can't seem to get it to work

    Thanks

    my code is

    string principal = this.Context.User.Identity.Name;

    string filter = string.Format("(&(ObjectClass={0})(sAMAccountName={1}))", "person", principal);

    string domain = "server1.domain.local";

    string[] properties = new string[] { "fullname" };

    DirectoryEntry adRoot = new DirectoryEntry("LDAP://" + domain, null, null, AuthenticationTypes.Secure);

    DirectorySearcher searcher = new DirectorySearcher(adRoot);

    searcher.SearchScope = SearchScope.Subtree;searcher.ReferralChasing = ReferralChasingOption.All;

    searcher.PropertiesToLoad.AddRange(properties);

    searcher.Filter = filter;

    SearchResult result = searcher.FindOne();

    DirectoryEntry directoryEntry = result.GetDirectoryEntry();

    string displayName = directoryEntry.Properties["displayName"][0].ToString();

    string email = directoryEntry.Properties["mail"][0].ToString();

    Tuesday, November 25, 2008 5:36 PM

All replies

  • User1055287656 posted

    Perhaps this will help.  I have written a complete ADMemebership provider, here are the two functions i use to get all users. 

     

    private MembershipUserCollection FindUsers(string Filter,DirectoryEntry SearchRoot, int PageIndex, int PageSize, out int Count)
            {
    			Count = 0;
    			SearchResultCollection dsUserSearchResult = null;
    			MembershipUserCollection RetVal = new MembershipUserCollection();
    			DirectorySearcher dsUser = new DirectorySearcher(SearchRoot, Filter);
    			dsUser.SearchScope = SearchScope.OneLevel;
    
    			try
    			{
    				if ((dsUserSearchResult = dsUser.FindAll()) != null)
    				{
    					Count = dsUserSearchResult.Count;
    					if (PageSize == PAGESIZE_NOPAGING) PageSize = Count;
    					int StartIndex = (PageIndex - 1) * PageSize;
    
    					for (int Idx = StartIndex; Idx < StartIndex + PageSize; Idx++)
    					{
    						DirectoryEntry deMembershipUser = dsUserSearchResult[Idx].GetDirectoryEntry();
    						deMembershipUser.RefreshCache();
                            ADMembershipUser user = new ADMembershipUser(deMembershipUser.Properties["objectSid"].Value, (string)deMembershipUser.Properties[attributeMapUsername].Value);
                            user.FirstName = (string)deMembershipUser.Properties["givenName"].Value;
                            user.LastName = (string)deMembershipUser.Properties["sn"].Value;
                            user.DisplayName = (string)deMembershipUser.Properties["displayName"].Value;
                            user.Street = (string)deMembershipUser.Properties["streetAddress"].Value;
                            user.PoBox = (string)deMembershipUser.Properties["postOfficeBox"].Value;
                            user.City = (string)deMembershipUser.Properties["l"].Value;
                            user.State = (string)deMembershipUser.Properties["st"].Value;
                            user.Zip = (string)deMembershipUser.Properties["postalCode"].Value;
                            user.Country = (string)deMembershipUser.Properties["c"].Value;
                            user.WwwHomePage = (string)deMembershipUser.Properties["wWWHomePage"].Value;
                            user.TelephoneNumber = (string)deMembershipUser.Properties["telephoneNumber"].Value;
                            user.TelephoneHome = (string)deMembershipUser.Properties["homePhone"].Value;
                            user.TelephoneIP = (string)deMembershipUser.Properties["ipPhone"].Value;
                            user.TelephoneMobile = (string)deMembershipUser.Properties["mobile"].Value;
                            user.TelephonePager = (string)deMembershipUser.Properties["pager"].Value;
                            user.TelephoneFax = (string)deMembershipUser.Properties["facsimileTelephoneNumber"].Value;
                            user.Office = (string)deMembershipUser.Properties["physicalDeliveryOfficeName"].Value;
                            user.Department = (string)deMembershipUser.Properties["department"].Value;
                            user.Company = (string)deMembershipUser.Properties["company"].Value;
                            user.Description = (string)deMembershipUser.Properties["description"].Value;
                            user.Notes = (string)deMembershipUser.Properties["info"].Value;
                            user.Title = (string)deMembershipUser.Properties["title"].Value;
                            RetVal.Add(user);
    						deMembershipUser.Close();
    					}
    
    				}
    
    				return RetVal;
    			}
    			catch (Exception Ex)
    			{
    				throw new ApplicationException("Error retrieving users", Ex);
    			}
            }
    And to call it all simply do this code:

    deUserContainer = new DirectoryEntry(connectionString, connectionUsername,connectionPassword,AuthenticationTypes.Secure|AuthenticationTypes.Sealing|AuthenticationTypes.ServerBind);

    MembershipUserCollection mUsers = FindUsers("(&(objectClass=person))", deUserContainer, pageIndex, pageSize, out totalRecords);

     

    You should be able to bind the collection to the grid.  Just in case you want to take my code and plop it right into your code  here is the ADMembershipUser class i use in my custom provider:

        [Serializable]
        [DataObject(true)] 
        public class ADMembershipUser : MembershipUser
        {
            private string firstName;
            private string lastName;
            private string displayName;
            private string description;
            private string office;
            private string telephoneNumber;
            private string wwwHomePage;
            private string street;
            private string poBox;
            private string city;
            private string state;
            private string zip;
            private string country;
            private string telephoneHome;
            private string telephonePager;
            private string telephoneMobile;
            private string telephoneFax;
            private string telephoneIP;
            private string title;
            private string department;
            private string company;
            private string notes;
            private string userName;
            private string email;
    
            private object providerUsersKey = null;
            
            public ADMembershipUser(object providerUserKey,string username) 
            {       
                this.providerUsersKey = providerUserKey;
                this.userName = username;
            }
    
            public ADMembershipUser(ADMembershipUser user, object providerUserKey, string username)
            {
                this.providerUsersKey = providerUserKey;
                this.userName = username;
    
                this.FirstName = user.FirstName;
                this.lastName = user.LastName;
                this.displayName = user.DisplayName;
                this.description = user.Description;
                this.office = user.Office;
                this.telephoneNumber = user.TelephoneNumber;
                this.wwwHomePage = user.WwwHomePage;
                this.street = user.Street;
                this.poBox = user.PoBox;
                this.city = user.City;
                this.state = user.State;
                this.zip = user.Zip;
                this.country = user.Country;
                this.telephoneHome = user.TelephoneHome;
                this.telephonePager = user.TelephonePager;
                this.telephoneMobile = user.TelephoneMobile;
                this.telephoneFax = user.TelephoneFax;
                this.telephoneIP = user.TelephoneIP;
                this.title = user.Title;
                this.department = user.Department;
                this.company = user.Company;
                this.notes = user.Notes;
                this.Email = user.Email;
            }
    
            [DataObjectField(true)]
            public override object ProviderUserKey
            {
                get
                {
                    return providerUsersKey;
                }
            }
    
            [DataObjectField(true)]
            public override string Email
            {
                get
                {
                    return email;
                }
                set
                {
                    email = value;
                }
            }
    
            [DataObjectField(true)]
            public override string UserName
            {
                get
                {
                    return this.userName;
                }
            }
    
            [DataObjectField(true)]
            public string Notes
            {
                get { return notes; }
                set { notes = value; }
            }
    
            [DataObjectField(true)]
            public string Key
            {
                get { return ((SecurityIdentifier)providerUsersKey).Value; }            
            }
    
            [DataObjectField(true)]
            public string FirstName
            {
                get { return firstName; }
                set { firstName = value; }
            }
    
            [DataObjectField(true)]
            public string LastName
            {
                get { return lastName; }
                set { lastName = value; }
            }
    
            [DataObjectField(true)]
            public string DisplayName
            {
                get { return displayName; }
                set { displayName = value; }
            }
    
            [DataObjectField(true)]
            public string Description
            {
                get { return description; }
                set { description = value; }
            }
    
            [DataObjectField(true)]
            public string Office
            {
                get { return office; }
                set { office = value; }
            }
    
            [DataObjectField(true)]
            public string TelephoneNumber
            {
                get { return telephoneNumber; }
                set { telephoneNumber = value; }
            }
    
            [DataObjectField(true)]
            public string WwwHomePage
            {
                get { return wwwHomePage; }
                set { wwwHomePage = value; }
            }
    
            [DataObjectField(true)]
            public string Street
            {
                get { return street; }
                set { street = value; }
            }
    
            [DataObjectField(true)]
            public string PoBox
            {
                get { return poBox; }
                set { poBox = value; }
            }
    
            [DataObjectField(true)]
            public string City
            {
                get { return city; }
                set { city = value; }
            }
    
            [DataObjectField(true)]
            public string State
            {
                get { return state; }
                set { state = value; }
            }
    
            [DataObjectField(true)]
            public string Zip
            {
                get { return zip; }
                set { zip = value; }
            }
    
            [DataObjectField(true)]
            public string Country
            {
                get { return country; }
                set { country = value; }
            }
    
            [DataObjectField(true)]
            public string TelephoneHome
            {
                get { return telephoneHome; }
                set { telephoneHome = value; }
            }
    
            [DataObjectField(true)]
            public string TelephonePager
            {
                get { return telephonePager; }
                set { telephonePager = value; }
            }
    
            [DataObjectField(true)]
            public string TelephoneMobile
            {
                get { return telephoneMobile; }
                set { telephoneMobile = value; }
            }
    
            [DataObjectField(true)]
            public string TelephoneFax
            {
                get { return telephoneFax; }
                set { telephoneFax = value; }
            }
    
            [DataObjectField(true)]
            public string TelephoneIP
            {
                get { return telephoneIP; }
                set { telephoneIP = value; }
            }
    
            [DataObjectField(true)]
            public string Title
            {
                get { return title; }
                set { title = value; }
            }
    
            [DataObjectField(true)]
            public string Department
            {
                get { return department; }
                set { department = value; }
            }
    
            [DataObjectField(true)]
            public string Company
            {
                get { return company; }
                set { company = value; }
            }
        }

     

    Tuesday, November 25, 2008 6:07 PM
  • User-1001485597 posted

    wow, thanks.  I will try this codes

    Dear chris, I saw that you have

      [Serializable]
        [DataObject(true)]

    what are those code for?  what is the namespace that I need to declare?  Thank you so much

    Tuesday, November 25, 2008 9:31 PM
  • User1055287656 posted

    the namespace for DataObject is:

    System.ComponentModel;

    the [DataObject] attribute marks the class as being an object suitable for binding to an ObjectDataSource control. [Serializable] attribute Indicates that a class can be serialized.

     

    Tuesday, November 25, 2008 9:54 PM
  • User-1001485597 posted

    I tried to narrow down my LDAP search string by search all users in Engineering group with email attribute.  but something wrong withmy search string

    can you help?  Thanks 

    DirectorySearcher search = new DirectorySearcher("LDAP://server1/CN=Engineering,DC=domain,DC=local");

    search.Filter = "(&(mail=*.com)(objectClass=user))";

    Wednesday, November 26, 2008 9:15 AM
  • User-1001485597 posted

    Ok, I already fixed my search filter. Thanks 

    search.Filter = "(&(objectCategory=user)(memberOf=CN=Engineering,CN=Users,DC=domain,DC=local))";

    Wednesday, November 26, 2008 10:47 AM
  • User218554976 posted

    Hello chris,

     

    the code you have provided gives the users from AD ,what if we want to get list of site users in wss3.0

    is it possible to modify this code to get list of site users in wss3.0 ??

    thanks again

    Tuesday, April 28, 2009 4:10 AM