public void QueryServicePrincipalTest() { using (UndoContext context = UndoContext.Current) { context.Start(); var client = (new GraphTestBase()).GraphClient; //test general 'list' var servicePrincipals = client.ServicePrincipal.List(null); Assert.NotNull(servicePrincipals); Assert.NotNull(servicePrincipals.StatusCode == HttpStatusCode.OK); Assert.NotNull(servicePrincipals.ServicePrincipals); string testServicePrincipalName = servicePrincipals.ServicePrincipals.ElementAt(0).ServicePrincipalNames[0]; string testObjcetId = servicePrincipals.ServicePrincipals.ElementAt(0).ObjectId; //test query by 'service principal name' ServicePrincipalListResult listResult = client.ServicePrincipal.GetByServicePrincipalName(testServicePrincipalName); ServicePrincipal servicePrincipal = listResult.ServicePrincipals[0]; Assert.True(listResult.ServicePrincipals.Count == 1); Assert.True(listResult.StatusCode == HttpStatusCode.OK); Assert.NotNull(servicePrincipal); Assert.True(servicePrincipal.ObjectId == testObjcetId); Assert.NotNull(servicePrincipal.DisplayName); Assert.NotNull(servicePrincipal.ObjectType); Assert.True(servicePrincipal.ServicePrincipalNames.Contains(testServicePrincipalName)); //test query by 'object id' ServicePrincipalGetResult getResult = client.ServicePrincipal.Get(testObjcetId); servicePrincipal = getResult.ServicePrincipal; Assert.NotNull(getResult); Assert.True(getResult.StatusCode == HttpStatusCode.OK); Assert.NotNull(getResult.ServicePrincipal); Assert.True(servicePrincipal.ObjectId == testObjcetId); Assert.NotNull(servicePrincipal.DisplayName); Assert.NotNull(servicePrincipal.ObjectType); Assert.True(servicePrincipal.ServicePrincipalNames.Contains(testServicePrincipalName)); //test query by 'displayName' listResult = client.ServicePrincipal.List(servicePrincipal.DisplayName); servicePrincipal = listResult.ServicePrincipals[0]; Assert.NotNull(listResult); Assert.True(listResult.StatusCode == HttpStatusCode.OK); Assert.True(servicePrincipal.ObjectId == testObjcetId); Assert.NotNull(servicePrincipal.DisplayName); Assert.NotNull(servicePrincipal.ObjectType); Assert.True(servicePrincipal.ServicePrincipalNames.Contains(testServicePrincipalName)); } }
public List <PSADServicePrincipal> FilterServicePrincipals(ADObjectFilterOptions options) { List <PSADServicePrincipal> servicePrincipals = new List <PSADServicePrincipal>(); ServicePrincipalListResult result = new ServicePrincipalListResult(); ServicePrincipal servicePrincipal = null; if (!string.IsNullOrEmpty(options.Id)) { try { servicePrincipal = GraphClient.ServicePrincipal.Get(options.Id).ServicePrincipal; } 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.ServicePrincipal.GetByServicePrincipalName(options.SPN).ServicePrincipals.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.ServicePrincipal.List(options.SearchString); } else { result = GraphClient.ServicePrincipal.ListNext(options.NextLink); } servicePrincipals.AddRange(result.ServicePrincipals.Select(u => u.ToPSADServicePrincipal())); options.NextLink = result.NextLink; } else { result = GraphClient.ServicePrincipal.List(options.SearchString); servicePrincipals.AddRange(result.ServicePrincipals.Select(u => u.ToPSADServicePrincipal())); while (!string.IsNullOrEmpty(result.NextLink)) { result = GraphClient.ServicePrincipal.ListNext(result.NextLink); servicePrincipals.AddRange(result.ServicePrincipals.Select(u => u.ToPSADServicePrincipal())); } } } return(servicePrincipals); }