public Guid GetObjectIdFromSPN(string spn)
        {
            var odataQueryFilter = new Rest.Azure.OData.ODataQuery <ServicePrincipal>(s => s.ServicePrincipalNames.Contains(spn));
            var sp = GraphClient.ServicePrincipals.List(odataQueryFilter.ToString()).SingleOrDefault();

            if (sp == null)
            {
                throw new InvalidOperationException(String.Format(ProjectResources.ServicePrincipalWithSPNDoesntExist, spn));
            }

            return(new Guid(sp.ObjectId));
        }
        // Temporary until this code has moved into ActiveDirectoryClient.
        private static string GetObjectIdFromUPN(ActiveDirectoryClient activeDirectoryClient, string upn)
        {
            var odataQueryFilter = new Rest.Azure.OData.ODataQuery <User>(s => s.UserPrincipalName == upn);
            var user             = activeDirectoryClient.GraphClient.Users.List(odataQueryFilter.ToString()).SingleOrDefault();

            if (user == null)
            {
                throw new InvalidOperationException(String.Format("User with UPN '{0}' does not exist.", upn));
            }

            return(user.ObjectId);
        }
        public Guid GetObjectIdFromUPN(string upn)
        {
            var odataQueryFilter = new Rest.Azure.OData.ODataQuery <User>(s => s.UserPrincipalName == upn);
            var user             = GraphClient.Users.List(odataQueryFilter.ToString()).SingleOrDefault();

            if (user == null)
            {
                throw new InvalidOperationException(String.Format(ProjectResources.UserWithUPNDoesntExist, upn));
            }

            return(new Guid(user.ObjectId));
        }
        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 IEnumerable <PSADServicePrincipal> FilterServicePrincipals(ADObjectFilterOptions options, ulong first = ulong.MaxValue, ulong skip = 0)
        {
            List <PSADServicePrincipal> servicePrincipals = new List <PSADServicePrincipal>();
            ServicePrincipal            servicePrincipal  = null;

            if (!string.IsNullOrEmpty(options.Id))
            {
                try
                {
                    servicePrincipal = GraphClient.ServicePrincipals.Get(options.Id);
                }
                catch { /* The user does not exist, ignore the exception. */ }

                if (servicePrincipal != null)
                {
                    servicePrincipals.Add(servicePrincipal.ToPSADServicePrincipal());
                }
            }
            else if (!string.IsNullOrEmpty(options.SPN))
            {
                try
                {
                    var odataQuery = new Rest.Azure.OData.ODataQuery <ServicePrincipal>(s => s.ServicePrincipalNames.Contains(options.SPN));
                    servicePrincipal = GraphClient.ServicePrincipals.List(odataQuery.ToString()).FirstOrDefault();
                }
                catch { /* The user does not exist, ignore the exception. */ }

                if (servicePrincipal != null)
                {
                    servicePrincipals.Add(servicePrincipal.ToPSADServicePrincipal());
                }
            }
            else
            {
                Rest.Azure.OData.ODataQuery <ServicePrincipal> odataQuery = null;
                if (!string.IsNullOrEmpty(options.SearchString) && options.SearchString.EndsWith("*"))
                {
                    options.SearchString = options.SearchString.TrimEnd('*');
                    odataQuery           = new Rest.Azure.OData.ODataQuery <ServicePrincipal>(s => s.DisplayName.StartsWith(options.SearchString));
                }
                else
                {
                    odataQuery = new Rest.Azure.OData.ODataQuery <ServicePrincipal>(s => s.DisplayName == options.SearchString);
                }

                return(FilterServicePrincipals(odataQuery, first, skip));
            }

            return(servicePrincipals);
        }
        public string GetObjectIdFromSignInName(string signInName)
        {
            if (string.IsNullOrEmpty(signInName))
            {
                return(null);
            }
            var odataQueryFilter = new Rest.Azure.OData.ODataQuery <User>(s => s.UserPrincipalName == signInName);
            var user             = _activeDirectoryClient.GraphClient.Users.List(odataQueryFilter.ToString()).SingleOrDefault();

            if (user == null)
            {
                throw new InvalidOperationException(String.Format(Resources.UserNameDoesNotExist, signInName));
            }
            return(user.ObjectId);
        }
        public Guid GetObjectIdFromServicePrincipalDisplayName(string displayName)
        {
            var odataQueryFilter = new Rest.Azure.OData.ODataQuery <ServicePrincipal>(s => s.DisplayName == displayName);
            var sp = GraphClient.ServicePrincipals.List(odataQueryFilter.ToString());

            if (sp == null || sp.FirstOrDefault() == null)
            {
                throw new InvalidOperationException(string.Format(ProjectResources.ServicePrincipalWithDisplayNameDoesntExist, displayName));
            }

            if (sp.Count() > 1)
            {
                throw new InvalidOperationException(string.Format(ProjectResources.MultipleServicePrincipalsWithDisplayNameFound, displayName));
            }

            return(new Guid(sp.FirstOrDefault().ObjectId));
        }
        public Guid GetUserObjectIdFromDisplayName(string displayName)
        {
            var odataQueryFilter = new Rest.Azure.OData.ODataQuery <User>(u => u.DisplayName == displayName);
            var user             = GraphClient.Users.List(odataQueryFilter.ToString());

            if (user == null || user.FirstOrDefault() == null)
            {
                throw new InvalidOperationException(string.Format(ProjectResources.UserWithDisplayNameDoesntExist, displayName));
            }

            if (user.Count() > 1)
            {
                throw new InvalidOperationException(string.Format(ProjectResources.MultipleUsersWithDisplayNameFound, displayName));
            }

            return(new Guid(user.FirstOrDefault().ObjectId));
        }
예제 #9
0
        public string GetObjectIdFromServicePrincipalName(string servicePrincipalName)
        {
            if (string.IsNullOrEmpty(servicePrincipalName))
            {
                return(null);
            }

            var odataQueryFilter = new Rest.Azure.OData.ODataQuery <ServicePrincipal>(s => s.ServicePrincipalNames.Contains(servicePrincipalName));
            var servicePrincipal = _activeDirectoryClient.GraphClient.ServicePrincipals.List(odataQueryFilter.ToString()).SingleOrDefault();

            if (servicePrincipal == null)
            {
                throw new AzPSInvalidOperationException(String.Format(Resources.ServicePrincipalNameDoesNotExist, servicePrincipalName));
            }

            return(servicePrincipal.ObjectId);
        }
        public List <PSADServicePrincipal> FilterServicePrincipals(ADObjectFilterOptions options)
        {
            List <PSADServicePrincipal> servicePrincipals = new List <PSADServicePrincipal>();
            IPage <ServicePrincipal>    result            = null;
            ServicePrincipal            servicePrincipal  = null;

            if (!string.IsNullOrEmpty(options.Id))
            {
                try
                {
                    servicePrincipal = GraphClient.ServicePrincipals.Get(options.Id);
                }
                catch { /* The user does not exist, ignore the exception. */ }

                if (servicePrincipal != null)
                {
                    servicePrincipals.Add(servicePrincipal.ToPSADServicePrincipal());
                }
            }
            else if (!string.IsNullOrEmpty(options.SPN))
            {
                try
                {
                    var odataQuery = new Rest.Azure.OData.ODataQuery <ServicePrincipal>(s => s.ServicePrincipalNames.Contains(options.SPN));
                    servicePrincipal = GraphClient.ServicePrincipals.List(odataQuery.ToString()).FirstOrDefault();
                }
                catch { /* The user does not exist, ignore the exception. */ }

                if (servicePrincipal != null)
                {
                    servicePrincipals.Add(servicePrincipal.ToPSADServicePrincipal());
                }
            }
            else
            {
                if (options.Paging)
                {
                    if (string.IsNullOrEmpty(options.NextLink))
                    {
                        var odataQuery = new Rest.Azure.OData.ODataQuery <ServicePrincipal>(s => s.DisplayName.StartsWith(options.SearchString));
                        result = GraphClient.ServicePrincipals.List(odataQuery);
                    }
                    else
                    {
                        result = GraphClient.ServicePrincipals.ListNext(options.NextLink);
                    }

                    servicePrincipals.AddRange(result.Select(u => u.ToPSADServicePrincipal()));
                    options.NextLink = result.NextPageLink;
                }
                else
                {
                    var odataQuery = new Rest.Azure.OData.ODataQuery <ServicePrincipal>(s => s.DisplayName.StartsWith(options.SearchString));
                    result = GraphClient.ServicePrincipals.List(odataQuery.ToString());
                    servicePrincipals.AddRange(result.Select(u => u.ToPSADServicePrincipal()));

                    while (!string.IsNullOrEmpty(result.NextPageLink))
                    {
                        result = GraphClient.ServicePrincipals.ListNext(result.NextPageLink);
                        servicePrincipals.AddRange(result.Select(u => u.ToPSADServicePrincipal()));
                    }
                }
            }

            return(servicePrincipals);
        }
 public IEnumerable <PSADApplication> GetApplicationWithFilters(Rest.Azure.OData.ODataQuery <Application> odataQueryFilter)
 {
     return(GraphClient.Applications.List(odataQueryFilter.ToString()).Select(a => a.ToPSADApplication()));
 }
        public List <PSADUser> FilterUsers(ADObjectFilterOptions options)
        {
            List <PSADUser> users  = new List <PSADUser>();
            IPage <User>    result = null;
            User            user   = null;

            if (!string.IsNullOrEmpty(options.Id))
            {
                try
                {
                    user = GraphClient.Users.Get(Normalize(options.Id));
                }
                catch { /* The user does not exist, ignore the exception. */ }

                if (user != null)
                {
                    users.Add(user.ToPSADUser());
                }
            }
            else if (!string.IsNullOrEmpty(options.UPN) || !string.IsNullOrEmpty(options.Mail))
            {
                try
                {
                    string upnOrMail  = Normalize(options.UPN) ?? Normalize(options.Mail);
                    var    odataQuery = new Rest.Azure.OData.ODataQuery <User>(u => u.UserPrincipalName == upnOrMail);
                    result = GraphClient.Users.List(odataQuery);
                }
                catch { /* The user does not exist, ignore the exception. */ }

                if (result != null)
                {
                    users.AddRange(result.Select(u => u.ToPSADUser()));
                }
            }
            else
            {
                if (options.Paging)
                {
                    if (string.IsNullOrEmpty(options.NextLink))
                    {
                        var odataQuery = new Rest.Azure.OData.ODataQuery <User>(u => u.DisplayName.StartsWith(options.SearchString));
                        result = GraphClient.Users.List(odataQuery.ToString());
                    }
                    else
                    {
                        result = GraphClient.Users.ListNext(options.NextLink);
                    }

                    users.AddRange(result.Select(u => u.ToPSADUser()));
                    options.NextLink = result.NextPageLink;
                }
                else
                {
                    var odataQuery = new Rest.Azure.OData.ODataQuery <User>(u => u.DisplayName.StartsWith(options.SearchString));
                    result = GraphClient.Users.List(odataQuery.ToString());
                    users.AddRange(result.Select(u => u.ToPSADUser()));

                    while (!string.IsNullOrEmpty(result.NextPageLink))
                    {
                        result = GraphClient.Users.ListNext(result.NextPageLink);
                        users.AddRange(result.Select(u => u.ToPSADUser()));
                    }
                }
            }

            return(users);
        }