private void GroupSearch(ADGroup branch, Principal group, PrincipalContext context)
        {
            GroupPrincipal grp = GroupPrincipal.FindByIdentity(context, IdentityType.SamAccountName, group.SamAccountName);
            PrincipalSearchResult <Principal> grouplist = grp.GetGroups();

            if (grouplist.Count <Principal>() > 0)
            {
                ADGroup maingroup = new ADGroup
                {
                    Groupname = group.Name
                };
                foreach (var groupmember in grouplist)
                {
                    GroupSearch(maingroup, groupmember, context);
                }
                groupList.Add(group.Name);
                branch.Subgroups.Add(maingroup);
            }
            else
            {
                groupList.Add(group.Name);
                branch.Subgroups.Add(new ADGroup()
                {
                    Groupname = group.Name
                });
            }
        }
Esempio n. 2
0
        public static void PrintGroup(GroupPrincipal group, bool showMembers = false, bool showGroups = false)
        {
            Console.WriteLine("Name: " + group.Name);
            Console.WriteLine("DistinguishedName: " + group.DistinguishedName);
            Console.WriteLine("DisplayName: " + group.DisplayName);
            Console.WriteLine("SamAccountName: " + group.SamAccountName);
            Console.WriteLine("UserPrincipalName: " + group.UserPrincipalName);
            Console.WriteLine("Description: " + group.Description);
            Console.WriteLine("IsSecurityGroup: " + group.IsSecurityGroup);
            //Console.WriteLine(": " + group.GroupScope.);
            Console.WriteLine("Guid: " + group.Guid);
            Console.WriteLine("Sid: " + group.Sid);

            if (showMembers)
            {
                Console.WriteLine();
                Console.WriteLine("Members:");
                if (group.Members.Count > 0)
                {
                    foreach (var user in group.Members.OrderBy(x => x.Name))
                    {
                        if (user is UserPrincipal)
                        {
                            Print.PrintUser(user as UserPrincipal, "");
                        }

                        if (user is GroupPrincipal)
                        {
                            Print.PrintGroup(group as GroupPrincipal);
                        }
                    }
                }
                else
                {
                    Console.WriteLine("\tNo Members");
                }
            }

            if (showGroups)
            {
                Console.WriteLine();
                Console.WriteLine("Member Of:");
                var groups = group.GetGroups().OrderBy(x => x.Name);
                if (groups.Count() > 0)
                {
                    foreach (GroupPrincipal g in groups)
                    {
                        Print.PrintGroup(g, false, false);
                    }
                }
                else
                {
                    Console.WriteLine("\tNo Membership");
                }
            }

            Console.WriteLine("===================================");
        }
Esempio n. 3
0
        public static List <adGroups> adObjectGroups(PrincipalContext context, string adObject, adObjectType objType, Boolean getNested)
        {
            List <adGroups> lstADGroups = new List <adGroups>();
            PrincipalSearchResult <Principal> groups = null;

            switch (objType)
            {
            case adObjectType.User:
                UserPrincipal user = UserPrincipal.FindByIdentity(context, adObject);
                if (user != null)
                {
                    if (getNested)
                    {
                        groups = user.GetAuthorizationGroups();
                    }
                    else
                    {
                        groups = user.GetGroups();
                    }
                }
                break;

            case adObjectType.Group:
                GroupPrincipal group = GroupPrincipal.FindByIdentity(context, adObject);
                if (group != null)
                {
                    groups = group.GetGroups();
                }
                break;
            }

            foreach (Principal p in groups)
            {
                adGroups adGroup = new adGroups();
                if (p is GroupPrincipal)
                {
                    adGroup.SammAccountName = p.SamAccountName;
                    adGroup.Name            = p.Name;
                    adGroup.DN = p.DistinguishedName;
                    lstADGroups.Add(adGroup);
                    //result.Add((GroupPrincipal)p);
                }
            }
            return(lstADGroups);
        }
Esempio n. 4
0
        public static bool CreateUser(string userLogonName)
        {
            try
            {
                // Creating the PrincipalContext
                PrincipalContext principalContext = null;
                principalContext = new PrincipalContext(ContextType.Domain, "yimihaodi.net", "CN=Users,DC=yimihaodi,DC=net");
                Console.WriteLine($"ConnectedServer:{principalContext.ConnectedServer},Container:{principalContext.Container},Name:{principalContext.Name},UserName:{principalContext.UserName}");

                GroupPrincipal groupPrincipal = new GroupPrincipal(principalContext);

                Console.WriteLine("get groups");
                var groups = groupPrincipal.GetGroups();
                foreach (var group in groups)
                {
                    Console.WriteLine($"Name:{group.Name},SamAccountName:{group.SamAccountName},DisplayName:{group.DisplayName},UserPrincipalName:{group.UserPrincipalName}");
                }

                Console.WriteLine("get members");
                var members = groupPrincipal.GetMembers();

                foreach (var member in members)
                {
                    Console.WriteLine($"Name:{member.Name},SamAccountName:{member.SamAccountName},DisplayName:{member.DisplayName},UserPrincipalName:{member.UserPrincipalName}");
                }

                UserPrincipal usr = UserPrincipal.FindByIdentity(principalContext, userLogonName);
                if (usr != null)
                {
                    Console.WriteLine(userLogonName + " already exists. Please use a different User Logon Name.");
                    return(false);
                }

                if (string.IsNullOrEmpty(userLogonName))
                {
                    Console.WriteLine($"userLogonName can not be null");
                    return(false);
                }

                // Create the new UserPrincipal object
                UserPrincipal userPrincipal = new UserPrincipal(principalContext);

                //userPrincipal.
                userPrincipal.SamAccountName = userLogonName;

                string password = "******";
                userPrincipal.SetPassword(password);

                userPrincipal.Enabled = true;

                userPrincipal.PasswordNeverExpires = true;

                userPrincipal.Save();
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception creating user object. " + e);
                return(false);
            }

            /***************************************************************
            *   The below code demonstrates on how you can make a smooth
            *   transition to DirectoryEntry from AccountManagement namespace,
            *   for advanced operations.
            ***************************************************************/
            //if (userPrincipal.GetUnderlyingObjectType() == typeof(DirectoryEntry))
            //{
            //    DirectoryEntry entry = (DirectoryEntry)userPrincipal.GetUnderlyingObject();
            //    if (address != null && address.Length > 0)
            //        entry.Properties["streetAddress"].Value = address;
            //    try
            //    {
            //        entry.CommitChanges();
            //    }
            //    catch (Exception e)
            //    {
            //        Console.WriteLine("Exception modifying address of the user. " + e);
            //        return false;
            //    }
            //}

            return(true);
        }