- 
      
 - 
        
Save jbartolome/995e460226845c3aa0ce96901668b77b to your computer and use it in GitHub Desktop.  
Revisions
- 
        
daniel-chambers revised this gist
Mar 15, 2015 . 1 changed file with 5 additions and 22 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -17,7 +17,7 @@ public static async Task<IEnumerable<IUser>> GetAllUsersInAppRoleAsync( .Select(a => a.PrincipalId.ToString()) .ToList(); var groupMembers = await client.GetAllUsersInGroupsAsync(groupObjectIds); var userObjectIds = appRoleAssignments .Where(a => a.Id == guidAppRoleId && a.PrincipalType == "User") @@ -35,17 +35,9 @@ public static async Task<IEnumerable<IUser>> GetAllUsersInAppRoleAsync( .ToList(); } public static async Task<IEnumerable<IUser>> GetAllUsersInGroupsAsync( this IActiveDirectoryClient client, IEnumerable<string> groupObjectIds) { var groupMembers = (await (await groupObjectIds @@ -55,16 +47,7 @@ private static async Task<IEnumerable<IUser>> GetAllUsersInGroupsRecursivelyAsyn .WhenAll()) .SelectMany(m => m); return groupMembers.OfType<IUser>().ToList(); } public static Task<IEnumerable<T>> EnumerateAllAsync<T>(  - 
        
daniel-chambers revised this gist
Mar 13, 2015 . 1 changed file with 4 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,9 @@ 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  - 
        
daniel-chambers created this gist
Mar 13, 2015 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,85 @@ 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.GetAllUsersInGroupsRecursivelyAsync(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 Task<IEnumerable<IUser>> GetAllUsersInGroupsRecursivelyAsync( this IActiveDirectoryClient client, IEnumerable<string> groupObjectIds) { return GetAllUsersInGroupsRecursivelyAsync(client, groupObjectIds, Enumerable.Empty<IUser>()); } private static async Task<IEnumerable<IUser>> GetAllUsersInGroupsRecursivelyAsync( this IActiveDirectoryClient client, IEnumerable<string> groupObjectIds, IEnumerable<IUser> previousUsers) { var groupMembers = (await (await groupObjectIds .Select(id => client.Groups.GetByObjectId(id).Members.ExecuteAsync()) .WhenAll()) .Select(c => c.EnumerateAllAsync()) .WhenAll()) .SelectMany(m => m); var users = previousUsers .Concat(groupMembers.OfType<IUser>().ToList()); var innerGroupObjectIds = groupMembers .OfType<IGroup>() .Select(g => g.ObjectId) .ToList(); return innerGroupObjectIds.Any() ? await GetAllUsersInGroupsRecursivelyAsync(client, innerGroupObjectIds, users) : users; } 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 hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,7 @@ public static class FunctionalExtensions { public static Task<T[]> WhenAll<T>(this IEnumerable<Task<T>> tasks) { return Task.WhenAll(tasks); } }