Last active
May 29, 2018 04:10
-
-
Save daniel-chambers/5e3702d40cd16c767ed1 to your computer and use it in GitHub Desktop.
Get All Users in AppRole in Azure Active Directory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class AadExtensions | |
{ | |
public static async Task<IEnumerable<IUser>> GetAllUsersInAppRoleAsync( | |
this IActiveDirectoryClient client, | |
string servicePrincipalObjectId, | |
string appRoleId) | |
{ | |
var guidAppRoleId = Guid.Parse(appRoleId); | |
var appRoleAssignmentsPaged = await client.ServicePrincipals | |
.GetByObjectId(servicePrincipalObjectId) | |
.AppRoleAssignedTo | |
.ExecuteAsync(); | |
var appRoleAssignments = await EnumerateAllAsync(appRoleAssignmentsPaged); | |
var groupObjectIds = appRoleAssignments | |
.Where(a => a.Id == guidAppRoleId && a.PrincipalType == "Group") | |
.Select(a => a.PrincipalId.ToString()) | |
.ToList(); | |
var groupMembers = await client.GetAllUsersInGroupsAsync(groupObjectIds); | |
var userObjectIds = appRoleAssignments | |
.Where(a => a.Id == guidAppRoleId && a.PrincipalType == "User") | |
.Select(a => a.PrincipalId.ToString()) | |
.ToList(); | |
var users = (await client.GetObjectsByObjectIdsAsync(userObjectIds, new[] { "User" })) | |
.Cast<IUser>() | |
.ToList(); | |
return groupMembers | |
.Concat(users) | |
.GroupBy(u => u.ObjectId) | |
.Select(g => g.First()) | |
.ToList(); | |
} | |
public static async Task<IEnumerable<IUser>> GetAllUsersInGroupsAsync( | |
this IActiveDirectoryClient client, | |
IEnumerable<string> groupObjectIds) | |
{ | |
var groupMembers = | |
(await (await groupObjectIds | |
.Select(id => client.Groups.GetByObjectId(id).Members.ExecuteAsync()) | |
.WhenAll()) | |
.Select(c => c.EnumerateAllAsync()) | |
.WhenAll()) | |
.SelectMany(m => m); | |
return groupMembers.OfType<IUser>().ToList(); | |
} | |
public static Task<IEnumerable<T>> EnumerateAllAsync<T>( | |
this IPagedCollection<T> pagedCollection) | |
{ | |
return EnumerateAllAsync(pagedCollection, Enumerable.Empty<T>()); | |
} | |
private static async Task<IEnumerable<T>> EnumerateAllAsync<T>( | |
this IPagedCollection<T> pagedCollection, | |
IEnumerable<T> previousItems) | |
{ | |
var newPreviousItems = previousItems.Concat(pagedCollection.CurrentPage); | |
if (pagedCollection.MorePagesAvailable == false) | |
return newPreviousItems; | |
var newPagedCollection = await pagedCollection.GetNextPageAsync(); | |
return await EnumerateAllAsync(newPagedCollection, newPreviousItems); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class FunctionalExtensions | |
{ | |
public static Task<T[]> WhenAll<T>(this IEnumerable<Task<T>> tasks) | |
{ | |
return Task.WhenAll(tasks); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment