public async Task <ActionResult> Index(GraphApiPage currentPage) { ClaimsPrincipal principal = HttpContext.User as ClaimsPrincipal; if (principal.Identity.IsAuthenticated) { string upn = principal.FindFirst("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/upn")?.Value; upn = HttpUtility.UrlEncode(upn); //var graphService = ServiceLocator.Current.GetInstance<GraphService>(); var graphProvider = new GraphAuthProvider(MemoryCache.Default); var graphService = new GraphService(MemoryCache.Default, new GraphSdkHelper(graphProvider), graphProvider); CompositeGraphObject compositeGraphObject = await graphService.GetUserJson(upn, null); currentPage.CompositeGraphObject = compositeGraphObject; return(View(PageViewModel.Create(currentPage))); } return(View()); }
// Load user's profile in formatted JSON. public async Task <CompositeGraphObject> GetUserJson(string email, string search = null) { if (email == null) { return(null); } CompositeGraphObject compositeGraphObject = this.memoryCache.Get("compositeGraphObject") as CompositeGraphObject; if (compositeGraphObject != null) { return(compositeGraphObject); } var user = HttpContext.Current.User as ClaimsPrincipal; string userId = user.FindFirst(Startup.ObjectIdentifierType)?.Value; var accessToken = await authProvider.GetUserAccessTokenAsync(userId); HttpClient httpClient = new HttpClient(new HttpClientHandler()); httpClient.BaseAddress = new Uri(@"https://graph.microsoft.com/beta/"); httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); try { User me = await GeObjectFromGraph <User>(httpClient, "me"); Organization organization = await GeObjectFromGraph <Organization>(httpClient, "organization"); //List<DriveItem> driveItems = await GetListOfObjectsFromGraph<DriveItem>(httpClient, "me/drive/root/children"); List <DriveItem> driveSharedItems = await GetListOfObjectsFromGraph <DriveItem>(httpClient, "me/drive/sharedWithMe"); //SHAREPOINT SHARED DOCUMENTS //List<List> siteLists = await GetListOfObjectsFromGraph<List>(httpClient, "sites/root/lists"); //List sharedDocuments = siteLists.FirstOrDefault(sr => sr.Name.Equals("Shared Documents")); //List<DriveItem> sharedDocumentItems = await GetListOfObjectsFromGraph<DriveItem>(httpClient, "sites/root/lists/" + sharedDocuments.Id + "/items"); List <Message> messages = await GetListOfObjectsFromGraph <Message>(httpClient, "me/messages"); List <Event> events = await GetListOfObjectsFromGraph <Event>(httpClient, "me/events"); List <Contact> contacts = await GetListOfObjectsFromGraph <Contact>(httpClient, "me/contacts"); List <OnenoteSection> oneNoteSections = await GetListOfObjectsFromGraph <OnenoteSection>(httpClient, "me/onenote/sections"); //List<OnenotePage> onenotePages = await GetListOfObjectsFromGraph<OnenotePage>(httpClient, "me/onenote/pages"); Planner planner = await GeObjectFromGraph <Planner>(httpClient, "me/planner/"); List <PlannerPlan> plannerPlan = await GetListOfObjectsFromGraph <PlannerPlan>(httpClient, "me/planner/plans"); List <PlannerTask> plannerTasks = await GetListOfObjectsFromGraph <PlannerTask>(httpClient, "me/planner/tasks"); List <Group> joinedTeams = await GetListOfObjectsFromGraph <Group>(httpClient, "me/joinedTeams"); string searchFor = search ?? "Wojciech"; List <User> foundUsers = await GetListOfObjectsFromGraph <User>(httpClient, "me/people?$search=" + searchFor); List <User> teamMembers = await GetListOfObjectsFromGraph <User>(httpClient, "groups/" + joinedTeams[0].Id + "/members"); List <TeamChannel> teamChannels = await GetListOfObjectsFromGraph <TeamChannel>(httpClient, "teams/" + joinedTeams[0].Id + "/channels"); List <TeamChannelTab> channelTabs = await GetListOfObjectsFromGraph <TeamChannelTab>(httpClient, "teams/" + joinedTeams[0].Id + "/channels/" + teamChannels[0].Id + "/tabs"); //List<object> applications = await GetListOfObjectsFromGraph<object>(httpClient, "applications"); List <AppRoleAssignment> appRoleAssignments = await GetListOfObjectsFromGraph <AppRoleAssignment>(httpClient, "me/appRoleAssignments"); var picture = await GetPictureBase64(userId); compositeGraphObject = new CompositeGraphObject() { Organization = organization, Planner = planner, TeamChannelTabs = channelTabs, User = me, Events = events, OnenoteSections = oneNoteSections, Messages = messages, PlannerPlans = plannerPlan, //OnenotePages = onenotePages, Contacts = contacts, AppRoleAssignments = appRoleAssignments, PlannerTasks = plannerTasks, TeamChannels = teamChannels, DriveItems = driveSharedItems, FoundUsers = foundUsers, JoinedTeams = joinedTeams, TeamMembers = teamMembers, Picture = picture }; this.memoryCache.Add(new CacheItem("compositeGraphObject", compositeGraphObject), new CacheItemPolicy() { SlidingExpiration = TimeSpan.FromHours(5), AbsoluteExpiration = DateTimeOffset.MaxValue }); return(compositeGraphObject); } catch (ServiceException e) { switch (e.Error.Code) { case "RequestResourceNotFound": case "ResourceNotFound": case "ErrorItemNotFound": case "itemNotFound": return(null); case "ErrorInvalidUser": return(null); case "AuthenticationFailure": return(null); case "TokenNotFound": //await httpContext.ChallengeAsync(); return(null); default: return(null); } } catch (Exception e) { return(null); } }