public async Task <ActionResult> Photo(string id, string userId)
        {
            // Initialize the GraphServiceClient.
            var graphClient = _graphSdkHelper.GetAuthenticatedClient(userId);

            string pic = default(string);

            try
            {
                pic = await GraphService.GetGroupPictureBase64(graphClient, id);
            }
            catch (ServiceException e)
            {
                return(Json(new { Message = "An unknown error has occurred." }));
            }

            System.Diagnostics.Debug.WriteLine(_msalLog.GetLog());

            return(Json(new { id = id, photoUrl = pic }));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Index(string email)
        {
            UserModel userModel = null;

            if (User.Identity.IsAuthenticated)
            {
                // Get user's id for token cache.
                var identifier = User.FindFirst(GraphAuthProvider.ObjectIdentifierType)?.Value;

                userModel = base.GetUserModelFromCache(identifier);

                if (userModel == null)
                {
                    // Get users's email.
                    email = email ?? User.Identity.Name ?? User.FindFirst("preferred_username").Value;

                    // Initialize the GraphServiceClient.
                    var graphClient = _graphSdkHelper.GetAuthenticatedClient(identifier);

                    var userData = await GraphService.GetUserJson(graphClient, email, HttpContext);

                    var userObject = JsonConvert.DeserializeObject <User>(userData);

                    userModel = new UserModel
                    {
                        Id    = identifier,
                        Name  = userObject.DisplayName,
                        Email = userObject.Mail
                    };

                    var pic = await GraphService.GetPictureBase64(graphClient, email, HttpContext);

                    userModel.PictureBase64 = pic;

                    // dont store an empty model
                    if (!string.IsNullOrEmpty(userModel.Name))
                    {
                        base.SaveUserModelInCache(userModel);
                    }
                }

                base.CopyUserModelToViewData(identifier);

                System.Diagnostics.Debug.WriteLine(_msalLog.GetLog());
            }

            return(View());
        }
        // GET: Group
        public async Task <ActionResult> Index()
        {
            List <GroupListItemViewModel> data = new List <GroupListItemViewModel>();

            // Get user's id for token cache.
            var identifier = User.FindFirst(GraphAuthProvider.ObjectIdentifierType)?.Value;

            base.CopyUserModelToViewData(identifier);

            // Initialize the GraphServiceClient.
            var graphClient = _graphSdkHelper.GetAuthenticatedClient(identifier);

            try
            {
                var groupsData = await GraphService.GetGroups(graphClient, HttpContext);

                foreach (var group in groupsData)
                {
                    data.Add(new GroupListItemViewModel
                    {
                        Key          = group.Id,
                        Description  = group.Description,
                        GroupType    = String.Join(" ", group.GroupTypes),
                        Name         = group.DisplayName,
                        MailNickname = group.MailNickname,
                        Thumbnail    = "",
                        Visibility   = group.Visibility
                    });
                }
            }
            catch (ServiceException e)
            {
                switch (e.Error.Code)
                {
                case "Authorization_RequestDenied":
                    return(new RedirectResult("/Account/PermissionsRequired"));

                default:
                    //return JsonConvert.SerializeObject(new { Message = "An unknown error has occurred." }, Formatting.Indented);
                    var x = e.ToString();
                    break;
                }
            }

            System.Diagnostics.Debug.WriteLine(_msalLog.GetLog());

            return(View(data));
        }
        public async Task <IActionResult> Index(string email)
        {
            UserModel userModel = null;

            if (User.Identity.IsAuthenticated)
            {
                // Get user's id for token cache.
                var identifier = User.FindFirst(Startup.ObjectIdentifierType)?.Value;

                userModel = base.GetUserModelFromCache(identifier);

                if (userModel == null)
                {
                    // Get users's email.
                    email = email ?? User.FindFirst("preferred_username").Value;

                    // Initialize the GraphServiceClient.
                    var graphClient = _graphSdkHelper.GetAuthenticatedClient((ClaimsIdentity)User.Identity);

                    try
                    {
                        var userData = await GraphService.GetUserJson(graphClient, email, HttpContext);

                        var userObject = JsonConvert.DeserializeObject <User>(userData);

                        userModel = new UserModel
                        {
                            Id    = identifier,
                            Name  = userObject.DisplayName,
                            Email = userObject.Mail
                        };

                        var pic = await GraphService.GetPictureBase64(graphClient, email, HttpContext);

                        userModel.PictureBase64 = pic;

                        // dont store an empty model
                        if (!string.IsNullOrEmpty(userModel.Name))
                        {
                            base.SaveUserModelInCache(userModel);
                        }
                    }
                    catch (ServiceException e)
                    {
                        switch (e.Error.Code)
                        {
                        case "Authorization_RequestDenied":
                            return(new RedirectResult("/Account/PermissionsRequired"));

                        default:
                            return(new RedirectResult($"/Home/Error?msg={e.Error.Message}"));
                        }
                    }
                }

                base.CopyUserModelToViewData(identifier);

                System.Diagnostics.Debug.WriteLine(_msalLog.GetLog());
            }

            return(View());
        }