public static PSADServicePrincipal ToPSADServicePrincipal(this ServicePrincipal servicePrincipal)
        {
            var adSp = new PSADServicePrincipal()
            {
                DisplayName           = servicePrincipal.DisplayName,
                ApplicationId         = Guid.Parse(servicePrincipal.AppId),
                ServicePrincipalNames = servicePrincipal.ServicePrincipalNames.ToArray(),
                Type = servicePrincipal.ObjectType
            };

            return((PSADServicePrincipal)AssignObjectId(adSp, servicePrincipal.ObjectId));
        }
        public PSADServicePrincipal GetServicePrincipalByObjectId(string objectId)
        {
            PSADServicePrincipal servicePrincipal = null;

            try
            {
                servicePrincipal = GraphClient.ServicePrincipals.Get(objectId).ToPSADServicePrincipal();
            }
            catch { /* The service principal does not exist, ignore the exception. */ }

            return(servicePrincipal);
        }
        public PSADServicePrincipal GetServicePrincipalBySPN(string spn)
        {
            PSADServicePrincipal servicePrincipal = null;

            try
            {
                var odataQuery = new Rest.Azure.OData.ODataQuery <ServicePrincipal>(s => s.ServicePrincipalNames.Contains(spn));
                servicePrincipal = GraphClient.ServicePrincipals.List(odataQuery.ToString()).FirstOrDefault()?.ToPSADServicePrincipal();
            }
            catch { /* The service principal does not exist, ignore the exception. */ }

            return(servicePrincipal);
        }
        public static PSADObject ToPSADObject(this AADObject obj)
        {
            if (obj == null)
            {
                throw new ArgumentNullException();
            }

            if (obj.ObjectType == typeof(User).Name)
            {
                var adUser = new PSADUser()
                {
                    DisplayName       = obj.DisplayName,
                    Type              = obj.ObjectType,
                    UserPrincipalName = obj.UserPrincipalName
                };

                return(AssignObjectId(adUser, obj.ObjectId));
            }
            else if (obj.ObjectType == "Group")
            {
                var adGroup = new PSADGroup()
                {
                    DisplayName     = obj.DisplayName,
                    Type            = obj.ObjectType,
                    SecurityEnabled = obj.SecurityEnabled,
                    MailNickname    = obj.Mail
                };
                return(AssignObjectId(adGroup, obj.ObjectId));
            }
            else if (obj.ObjectType == typeof(ServicePrincipal).Name)
            {
                var adSp = new PSADServicePrincipal()
                {
                    DisplayName           = obj.DisplayName,
                    Type                  = obj.ObjectType,
                    ServicePrincipalNames = obj.ServicePrincipalNames.ToArray()
                };

                return(AssignObjectId(adSp, obj.ObjectId));
            }
            else
            {
                var adObj = new PSADObject()
                {
                    DisplayName = obj.DisplayName,
                    Type        = obj.ObjectType
                };

                return(AssignObjectId(adObj, obj.ObjectId));
            }
        }
        public PSADServicePrincipal RemoveServicePrincipal(Guid objectId)
        {
            var objectIdString = objectId.ToString();
            PSADServicePrincipal servicePrincipal = FilterServicePrincipals(new ADObjectFilterOptions()
            {
                Id = objectId.ToString()
            }).FirstOrDefault();

            if (servicePrincipal != null)
            {
                GraphClient.ServicePrincipals.Delete(objectIdString);
            }
            else
            {
                throw new KeyNotFoundException(string.Format(ProjectResources.ServicePrincipalDoesntExist, objectId));
            }

            return(servicePrincipal);
        }