public async Task <ActionResult> Share(string id) { AuthenticationHelper authHelper = new AuthenticationHelper(ConfigHelper.Authority, new ADALTokenCache(Util.GetSignedInUsersObjectIdFromClaims())); // Values Needed for the People Picker ViewData["tenant"] = ConfigHelper.TenantId; ViewData["token"] = await authHelper.GetOnBehalfOfAccessToken(ConfigHelper.GraphResourceId, ConfigHelper.PostLogoutRedirectUri); UserGroupsAndDirectoryRoles userGroupsAndDirectoryRoles = await TokenHelper.GetUsersGroupsAsync(ClaimsPrincipal.Current); List <string> userGroupsAndId = userGroupsAndDirectoryRoles.GroupIds; string userObjectId = Util.GetSignedInUsersObjectIdFromClaims(); userGroupsAndId.Add(userObjectId); ViewData["tasks"] = TasksDbHelper.GetAllTasks(userGroupsAndId); ViewData["userId"] = userObjectId; // Get the task details WebApp_GroupClaims_DotNet.Models.Task task = TasksDbHelper.GetTask(Convert.ToInt32(id)); if (task == null) { RedirectToAction("ShowError", "Error", new { message = "Task Not Found in DB." }); } ViewData["shares"] = task.SharedWith.ToList(); ViewData["taskText"] = task.TaskText; ViewData["taskId"] = task.TaskID; return(View()); }
// GET: UserProfile public async Task <ActionResult> Index() { try { MSGraphClient msGraphClient = new MSGraphClient(ConfigHelper.Authority, new ADALTokenCache(Util.GetSignedInUsersObjectIdFromClaims())); User user = await msGraphClient.GetMeAsync(); UserGroupsAndDirectoryRoles userGroupsAndDirectoryRoles = await msGraphClient.GetCurrentUserGroupsAndRolesAsync(); //IList<Group> groups = await msGraphClient.GetCurrentUserGroupsAsync(); //IList<DirectoryRole> directoryRoles = await msGraphClient.GetCurrentUserDirectoryRolesAsync(); ViewData["overageOccurred"] = userGroupsAndDirectoryRoles.HasOverageClaim; ViewData["myGroups"] = userGroupsAndDirectoryRoles.Groups; ViewData["myDirectoryRoles"] = userGroupsAndDirectoryRoles.DirectoryRoles; return(View(user)); } catch (AdalException) { // Return to error page. return(View("Error")); } // if the above failed, the user needs to explicitly re-authenticate for the app to obtain the required token catch (Exception) { return(View("Relogin")); } }
/// <summary> /// A more efficient implementation that gets both group and role membership in one call /// </summary> /// <returns></returns> public async Task <UserGroupsAndDirectoryRoles> GetCurrentUserGroupsAndRolesAsync() { UserGroupsAndDirectoryRoles userGroupsAndDirectoryRoles = new UserGroupsAndDirectoryRoles(); IUserMemberOfCollectionWithReferencesPage memberOfDirectoryRoles = null; try { GraphServiceClient graphClient = this.GetAuthenticatedClientForUser(); memberOfDirectoryRoles = await graphClient.Me.MemberOf.Request().GetAsync(); if (memberOfDirectoryRoles != null) { do { foreach (var directoryObject in memberOfDirectoryRoles.CurrentPage) { if (directoryObject is Group) { Group group = directoryObject as Group; Trace.WriteLine($"Got group: {group.Id}- '{group.DisplayName}'"); userGroupsAndDirectoryRoles.Groups.Add(group); } else if (directoryObject is DirectoryRole) { DirectoryRole role = directoryObject as DirectoryRole; Trace.WriteLine($"Got DirectoryRole: {role.Id}- '{role.DisplayName}'"); userGroupsAndDirectoryRoles.DirectoryRoles.Add(role); } } if (memberOfDirectoryRoles.NextPageRequest != null) { userGroupsAndDirectoryRoles.HasOverageClaim = true; memberOfDirectoryRoles = await memberOfDirectoryRoles.NextPageRequest.GetAsync(); } else { memberOfDirectoryRoles = null; } } while (memberOfDirectoryRoles != null); } return(userGroupsAndDirectoryRoles); } catch (ServiceException e) { Trace.Fail("We could not get user groups and roles: " + e.Error.Message); return(null); } }
public static async Task <UserGroupsAndDirectoryRoles> GetUsersGroupsAsync(ClaimsPrincipal subject) { UserGroupsAndDirectoryRoles userGroupsAndDirectoryRoles = new UserGroupsAndDirectoryRoles(); userGroupsAndDirectoryRoles.HasOverageClaim = HasGroupsOverageClaim(subject); ClaimsIdentity userClaimsId = subject.Identity as ClaimsIdentity; if (userGroupsAndDirectoryRoles.HasOverageClaim) { userGroupsAndDirectoryRoles.GroupIds.AddRange(await GetUsersGroupsFromClaimSourcesAsync(userClaimsId)); } else { userGroupsAndDirectoryRoles.GroupIds.AddRange(userClaimsId.FindAll(SubjectAttribute.Groups).Select(c => c.Value).ToList()); } return(userGroupsAndDirectoryRoles); }
public async Task <ActionResult> Index() { try { // Get All Tasks User Can View ClaimsIdentity userClaimsId = ClaimsPrincipal.Current.Identity as ClaimsIdentity; UserGroupsAndDirectoryRoles userGroupsAndDirectoryRoles = await TokenHelper.GetUsersGroupsAsync(ClaimsPrincipal.Current); List <string> userGroupsAndId = userGroupsAndDirectoryRoles.GroupIds; string userObjectId = Util.GetSignedInUsersObjectIdFromClaims(); userGroupsAndId.Add(userObjectId); ViewData["tasks"] = TasksDbHelper.GetAllTasks(userGroupsAndId); ViewData["userId"] = userObjectId; return(View()); } catch (Exception e) { // Catch Both ADAL Exceptions and Web Exceptions return(RedirectToAction("ShowError", "Error", new { errorMessage = e.Message })); } }