public List<PSADServicePrincipal> FilterServicePrincipals(ADObjectFilterOptions options)
{
List<PSADServicePrincipal> servicePrincipals = new List<PSADServicePrincipal>();
Rest.Azure.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
{
servicePrincipal = GraphClient.ServicePrincipals.List(new Rest.Azure.OData.ODataQuery<ServicePrincipal>(s => s.ServicePrincipalNames.Contains(options.SPN))).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))
{
result = GraphClient.ServicePrincipals.List(new Rest.Azure.OData.ODataQuery<ServicePrincipal>(s => s.DisplayName.StartsWith(options.SearchString)));
}
else
{
result = GraphClient.ServicePrincipals.ListNext(options.NextLink);
}
servicePrincipals.AddRange(result.Select(u => u.ToPSADServicePrincipal()));
options.NextLink = result.NextPageLink;
}
else
{
result = GraphClient.ServicePrincipals.List(new Rest.Azure.OData.ODataQuery<ServicePrincipal>(s => s.DisplayName.StartsWith(options.SearchString)));
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;
}