Click here to Skip to main content
15,910,009 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I want get all user from particular security group. This Group present in Active directory.

I want filter condition for this and also I need C sharp code to do it same.

I am trying following Filter criteria:

(&(objectCategory=group)(SAMAccountName=" + txtusername.Text + "))


But it return only security group, I want user of this security group.


Thanks in Advance.
Posted

C#
class ActiveDirectory
    {
        public ActiveDirectory()
        {
            //PrincipalContext insPrincipalContext = new PrincipalContext(ContextType.Machine); //Connecting to local computer.
            //PrincipalContext insPrincipalContext = new PrincipalContext(ContextType.Domain, "MPTDC1-INDIA", "DC=MPTDC1-INDIA,DC=com"); //Connecting to Active Directory
            //PrincipalContext insPrincipalContext = new PrincipalContext(ContextType.Machine, "TAMERO", "administrator", "password"); //Connecting to local computer with credentials of an user

        }

        public void GetUsers()
        {
            PrincipalContext insPrincipalContext = new PrincipalContext(ContextType.Domain, "MPTDC1-INDIA");

            UserPrincipal insUserPrincipal = new UserPrincipal(insPrincipalContext);
            insUserPrincipal.Name = "*";
            SearchUsers(insUserPrincipal);
        }

        private void SearchUsers(UserPrincipal parUserPrincipal)
        {
            PrincipalSearcher insPrincipalSearcher = new PrincipalSearcher();
            insPrincipalSearcher.QueryFilter = parUserPrincipal;
            PrincipalSearchResult<Principal> results = insPrincipalSearcher.FindAll();
            foreach (Principal p in results)
            {
                Console.WriteLine(p.DisplayName);
            }
        }
    }

class Program
    {
        static void Main(string[] args)
        {
            var ad = new ActiveDirectory();
            ad.GetUsers();
        }
    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900