示例#1
0
        public List <PSADObject> ListUserGroups(string principal)
        {
            List <PSADObject> result = new List <PSADObject>();
            Guid objectId            = GetObjectId(new ADObjectFilterOptions {
                UPN = principal
            });
            PSADObject user = GetADObject(new ADObjectFilterOptions {
                Id = objectId.ToString()
            });
            var groupsIds    = GraphClient.Users.GetMemberGroups(objectId.ToString(), new UserGetMemberGroupsParameters());
            var groupsResult = GraphClient.Objects.GetObjectsByObjectIds(new GetObjectsParameters {
                ObjectIds = groupsIds.ToList()
            });

            result.AddRange(groupsResult.Select(g => g.ToPSADGroup()));

            return(result);
        }
        public List <PSADObject> GetGroupMembers(ADObjectFilterOptions options)
        {
            List <PSADObject> members = new List <PSADObject>();
            PSADObject        group   = FilterGroups(options).FirstOrDefault();

            if (group != null)
            {
                GetObjectsResult result = new GetObjectsResult();

                if (options.Paging)
                {
                    if (string.IsNullOrEmpty(options.NextLink))
                    {
                        result = GraphClient.Group.GetGroupMembers(group.Id.ToString());
                    }
                    else
                    {
                        result = GraphClient.Group.GetGroupMembersNext(result.NextLink);
                    }

                    members.AddRange(result.AADObject.Select(u => u.ToPSADObject()));
                    options.NextLink = result.NextLink;
                }
                else
                {
                    result = GraphClient.Group.GetGroupMembers(group.Id.ToString());
                    members.AddRange(result.AADObject.Select(u => u.ToPSADObject()));

                    while (!string.IsNullOrEmpty(result.NextLink))
                    {
                        result = GraphClient.Group.GetGroupMembersNext(result.NextLink);
                        members.AddRange(result.AADObject.Select(u => u.ToPSADObject()));
                    }
                }
            }

            return(members);
        }
        public PSADObject GetADObject(ADObjectFilterOptions options)
        {
            PSADObject result = null;

            Debug.Assert(options != null);

            if (IsSet(options.SignInName, options.Mail, options.UPN, options.Id))
            {
                result = FilterUsers(options).FirstOrDefault();
            }

            if (result == null && IsSet(options.SPN, options.Id))
            {
                result = FilterServicePrincipals(options).FirstOrDefault();
            }

            if (result == null && IsSet(options.Mail, options.Id))
            {
                result = FilterGroups(options).FirstOrDefault();
            }

            return(result);
        }
        public Guid GetObjectId(ADObjectFilterOptions options)
        {
            Guid principalId;

            if (options != null && options.Id != null &&
                Guid.TryParse(options.Id, out principalId))
            {
                // do nothing, we have parsed the guid
            }
            else
            {
                PSADObject adObj = GetADObject(options);

                if (adObj == null)
                {
                    throw new KeyNotFoundException("The provided information does not map to an AD object id.");
                }

                principalId = adObj.Id;
            }

            return(principalId);
        }