コード例 #1
0
        /// <summary>
        /// Gets details of a single <see cref="Contact"/> Graph.
        /// </summary>
        /// <returns>A view with the details of a single <see cref="Contact"/>.</returns>
        public ActionResult Details(string objectId)
        {
            //Get the access token as we need it to make a call to the Graph API
            string accessToken = AuthUtils.GetAuthToken(Request, HttpContext);

            if (accessToken == null)
            {
                //
                // The user needs to re-authorize.  Show them a message to that effect.
                //
                ViewBag.ErrorMessage = "AuthorizationRequired";
                return(View());
            }

            // Setup Graph API connection and get single Contact
            Guid          ClientRequestId = Guid.NewGuid();
            GraphSettings graphSettings   = new GraphSettings();

            graphSettings.ApiVersion = GraphConfiguration.GraphApiVersion;
            GraphConnection graphConnection = new GraphConnection(accessToken, ClientRequestId, graphSettings);

            Contact contact = graphConnection.Get <Contact>(objectId);

            return(View(contact));
        }
コード例 #2
0
        public ActionResult Create([Bind(Include = "UserPrincipalName,AccountEnabled,PasswordProfile,MailNickname,DisplayName,GivenName,Surname,JobTitle,Department")] User user)
        {
            //Get the access token as we need it to make a call to the Graph API
            var accessToken = AuthUtils.GetAuthToken(Request, HttpContext);

            if (accessToken == null)
            {
                //
                // The user needs to re-authorize.  Show them a message to that effect.
                //
                ViewBag.ErrorMessage = "AuthorizationRequired";
                return(View());
            }

            try
            {
                // Setup Graph API connection and add User
                var clientRequestId = Guid.NewGuid();
                var graphSettings   = new GraphSettings {
                    ApiVersion = GraphConfiguration.GraphApiVersion
                };
                var graphConnection = new GraphConnection(accessToken, clientRequestId, graphSettings);

                graphConnection.Add(user);
                return(RedirectToAction("Index"));
            }
            catch (Exception exception)
            {
                ModelState.AddModelError("", exception.Message);
                return(View());
            }
        }
コード例 #3
0
        /// <summary>
        /// Gets a list of <see cref="Contact"/> objects from Graph.
        /// </summary>
        /// <returns>A view with the list of <see cref="Contact"/> objects.</returns>
        public ActionResult Index()
        {
            //Get the access token as we need it to make a call to the Graph API
            string accessToken = AuthUtils.GetAuthToken(Request, HttpContext);

            if (accessToken == null)
            {
                //
                // The user needs to re-authorize.  Show them a message to that effect.
                //
                ViewBag.ErrorMessage = "AuthorizationRequired";
                return(View());
            }

            //Setup Graph API connection and get a list of users
            Guid          ClientRequestId = Guid.NewGuid();
            GraphSettings graphSettings   = new GraphSettings();

            graphSettings.ApiVersion = GraphConfiguration.GraphApiVersion;
            GraphConnection graphConnection = new GraphConnection(accessToken, ClientRequestId, graphSettings);

            PagedResults <Contact> pagedResults = graphConnection.List <Contact>(null, new FilterGenerator());

            return(View(pagedResults.Results));
        }
コード例 #4
0
        /// <summary>
        /// Gets a list of <see cref="User"/> objects that a given <see cref="User"/> has as a direct report.
        /// </summary>
        /// <param name="objectId">Unique identifier of the <see cref="User"/>.</param>
        /// <returns>A view with the list of <see cref="User"/> objects.</returns>
        public ActionResult GetDirectReports(string objectId)
        {
            //Get the access token as we need it to make a call to the Graph API
            string accessToken = AuthUtils.GetAuthToken(Request, HttpContext);

            if (accessToken == null)
            {
                //
                // The user needs to re-authorize.  Show them a message to that effect.
                //
                ViewBag.ErrorMessage = "AuthorizationRequired";
                return(View());
            }

            // Setup Graph API connection and get Group membership
            Guid          ClientRequestId = Guid.NewGuid();
            GraphSettings graphSettings   = new GraphSettings();

            graphSettings.ApiVersion = GraphConfiguration.GraphApiVersion;
            GraphConnection graphConnection = new GraphConnection(accessToken, ClientRequestId, graphSettings);

            GraphObject         graphUser = graphConnection.Get <User>(objectId);
            IList <GraphObject> results   = graphConnection.GetAllDirectLinks(graphUser, LinkProperty.DirectReports);
            IList <User>        reports   = new List <User>();

            foreach (GraphObject obj in results)
            {
                if (obj is User)
                {
                    User user = (User)obj;
                    reports.Add(user);
                }
            }
            return(View(reports));
        }
コード例 #5
0
        /// <summary>
        /// Creates a view to for editing an existing <see cref="User"/> in Graph.
        /// </summary>
        /// <param name="objectId">Unique identifier of the <see cref="User"/>.</param>
        /// <returns>A view with details to edit <see cref="User"/>.</returns>
        public ActionResult Edit(string objectId)
        {
            //Get the access token as we need it to make a call to the Graph API
            string accessToken = AuthUtils.GetAuthToken(Request, HttpContext);

            if (accessToken == null)
            {
                //
                // The user needs to re-authorize.  Show them a message to that effect.
                //
                ViewBag.ErrorMessage = "AuthorizationRequired";
                return(View());
            }

            // Setup Graph API connection and get single User
            var clientRequestId = Guid.NewGuid();
            var graphSettings   = new GraphSettings {
                ApiVersion = GraphConfiguration.GraphApiVersion
            };
            var graphConnection = new GraphConnection(accessToken, clientRequestId, graphSettings);

            var user = graphConnection.Get <User>(objectId);

            return(View(user));
        }
コード例 #6
0
        public ActionResult Delete(User user)
        {
            //Get the access token as we need it to make a call to the Graph API
            string accessToken = AuthUtils.GetAuthToken(Request, HttpContext);

            if (accessToken == null)
            {
                //
                // The user needs to re-authorize.  Show them a message to that effect.
                //
                ViewBag.ErrorMessage = "AuthorizationRequired";
                return(View());
            }

            try
            {
                // Setup Graph API connection and delete User
                var clientRequestId = Guid.NewGuid();
                var graphSettings   = new GraphSettings {
                    ApiVersion = GraphConfiguration.GraphApiVersion
                };
                var graphConnection = new GraphConnection(accessToken, clientRequestId, graphSettings);
                graphConnection.Delete(user);
                return(RedirectToAction("Index"));
            }
            catch (Exception exception)
            {
                ModelState.AddModelError("", exception.Message);
                return(View(user));
            }
        }
コード例 #7
0
        /// <summary>
        /// Creates a view to delete an existing <see cref="User"/>.
        /// </summary>
        /// <param name="objectId">Unique identifier of the <see cref="User"/>.</param>
        /// <returns>A view of the <see cref="User"/> to be deleted.</returns>
        public ActionResult Delete(string objectId)
        {
            //Get the access token as we need it to make a call to the Graph API
            string accessToken = AuthUtils.GetAuthToken(Request, HttpContext);

            if (accessToken == null)
            {
                //
                // The user needs to re-authorize.  Show them a message to that effect.
                //
                ViewBag.ErrorMessage = "AuthorizationRequired";
                return(View());
            }

            try
            {
                // Setup Graph API connection and get single User
                Guid          ClientRequestId = Guid.NewGuid();
                GraphSettings graphSettings   = new GraphSettings();
                graphSettings.ApiVersion = GraphConfiguration.GraphApiVersion;
                GraphConnection graphConnection = new GraphConnection(accessToken, ClientRequestId, graphSettings);
                User            user            = graphConnection.Get <User>(objectId);
                return(View(user));
            }
            catch (Exception exception)
            {
                ModelState.AddModelError("", exception.Message);
                return(View());
            }
        }
コード例 #8
0
        public ActionResult Create([Bind(Include = "DisplayName,Description,MailNickName,SecurityEnabled")] Group group)
        {
            //Get the access token as we need it to make a call to the Graph API
            string accessToken = AuthUtils.GetAuthToken(Request, HttpContext);

            if (accessToken == null)
            {
                //
                // The user needs to re-authorize.  Show them a message to that effect.
                //
                ViewBag.ErrorMessage = "AuthorizationRequired";
                return(View());
            }

            try
            {
                // Setup Graph API connection and add Group
                Guid          ClientRequestId = Guid.NewGuid();
                GraphSettings graphSettings   = new GraphSettings();
                graphSettings.ApiVersion = GraphConfiguration.GraphApiVersion;
                GraphConnection graphConnection = new GraphConnection(accessToken, ClientRequestId, graphSettings);
                group.MailEnabled = false;

                graphConnection.Add(group);
                return(RedirectToAction("Index"));
            }
            catch (Exception exception)
            {
                ModelState.AddModelError("", exception.Message);
                return(View());
            }
        }
コード例 #9
0
        public ActionResult Edit([Bind(Include = "ObjectId,UserPrincipalName,DisplayName,AccountEnabled,GivenName,Surname,JobTitle,Department,Mobile,StreetAddress,City,State,Country,")] User user, FormCollection values)
        {
            //Get the access token as we need it to make a call to the Graph API
            string accessToken = AuthUtils.GetAuthToken(Request, HttpContext);

            if (accessToken == null)
            {
                //
                // The user needs to re-authorize.  Show them a message to that effect.
                //
                ViewBag.ErrorMessage = "AuthorizationRequired";
                return(View());
            }

            try
            {
                // TODO: Add update logic here

                // Setup Graph API connection and update single User
                var clientRequestId = Guid.NewGuid();
                var graphSettings   = new GraphSettings {
                    ApiVersion = GraphConfiguration.GraphApiVersion
                };
                var graphConnection = new GraphConnection(accessToken, clientRequestId, graphSettings);
                graphConnection.Update(user);

                // update thumbnail photo



                if (!String.IsNullOrEmpty(values["photofile"]))
                {
                    //string path = AppDomain.CurrentDomain.BaseDirectory + "uploads/";
                    //string filename = Path.GetFileName(values["photofile"]);
                    //Image image = Image.FromFile(filename);

                    var imageFile = Path.Combine(Server.MapPath("~/app_data"), values["photofile"]);
                    var image     = Image.FromFile(imageFile);

                    var stream = new MemoryStream();
                    image.Save(stream, ImageFormat.Jpeg);

                    // Write the photo file to the Graph service.
                    graphConnection.SetStreamProperty(user, GraphProperty.ThumbnailPhoto, stream, "image/jpeg");
                }


                return(RedirectToAction("Index"));
            }
            catch (Exception exception)
            {
                ModelState.AddModelError("", exception.Message);
                return(View());
            }
        }
コード例 #10
0
        public ActionResult ShowThumbnail(string id)
        {
            string accessToken = AuthUtils.GetAuthToken(Request, HttpContext);

            if (accessToken == null)
            {
                //
                // The user needs to re-authorize.  Show them a message to that effect.
                //
                ViewBag.ErrorMessage = "AuthorizationRequired";
                return(View());
            }

            // Setup Graph API connection and get Group membership
            Guid          ClientRequestId = Guid.NewGuid();
            GraphSettings graphSettings   = new GraphSettings();

            graphSettings.ApiVersion = GraphConfiguration.GraphApiVersion;
            GraphConnection graphConnection = new GraphConnection(accessToken, ClientRequestId, graphSettings);

            //  User user = graphConnection.Get<User>(id);
            User user = new User();

            user.ObjectId = id;

            try
            {
                Stream ms = graphConnection.GetStreamProperty(user, GraphProperty.ThumbnailPhoto, "image/jpeg");
                user.ThumbnailPhoto = ms;
            }
            catch
            {
                user.ThumbnailPhoto = null;
            }


            if (user.ThumbnailPhoto != null)
            {
                return(File(user.ThumbnailPhoto, "image/jpeg"));
            }

            return(View());
        }
コード例 #11
0
        /// <summary>
        /// Gets a list of <see cref="Group"/> objects that a given <see cref="User"/> is member of.
        /// </summary>
        /// <param name="objectId">Unique identifier of the <see cref="User"/>.</param>
        /// <returns>A view with the list of <see cref="Group"/> objects.</returns>
        public ActionResult GetGroups(string objectId)
        {
            //Get the access token as we need it to make a call to the Graph API
            string accessToken = AuthUtils.GetAuthToken(Request, HttpContext);

            if (accessToken == null)
            {
                //
                // The user needs to re-authorize.  Show them a message to that effect.
                //
                ViewBag.ErrorMessage = "AuthorizationRequired";
                return(View());
            }
            // Setup Graph API connection and get Group membership
            var clientRequestId = Guid.NewGuid();
            var graphSettings   = new GraphSettings {
                ApiVersion = GraphConfiguration.GraphApiVersion
            };
            var graphConnection = new GraphConnection(accessToken, clientRequestId, graphSettings);

            GraphObject graphUser  = graphConnection.Get <User>(objectId);
            var         memberShip = graphConnection.GetLinkedObjects(graphUser, LinkProperty.MemberOf, null, 999);

            // users can be members of Groups and Roles
            // for this app, we will filter and only show Groups

            var           count           = 0;
            IList <Group> groupMembership = new List <Group>();

            foreach (var graphObj in memberShip.Results)
            {
                if (graphObj.ODataTypeName.Contains("Group"))
                {
                    var groupMember = (Group)memberShip.Results[count];
                    groupMembership.Add(groupMember);
                }
                ++count;
            }

            //return View(groupIds);
            return(View(groupMembership));
        }
コード例 #12
0
        /// <summary>
        /// Gets a list of <see cref="Group"/> objects that a given <see cref="Contact"/> is member of.
        /// </summary>
        /// <param name="objectId">Unique identifier of the <see cref="Contact"/>.</param>
        /// <returns>A view with the list of <see cref="Group"/> objects.</returns>
        public ActionResult GetGroups(string objectId)
        {
            //Get the access token as we need it to make a call to the Graph API
            string accessToken = AuthUtils.GetAuthToken(Request, HttpContext);

            if (accessToken == null)
            {
                //
                // The user needs to re-authorize.  Show them a message to that effect.
                //
                ViewBag.ErrorMessage = "AuthorizationRequired";
                return(View());
            }

            // Setup Graph API connection and get Group membership
            Guid          ClientRequestId = Guid.NewGuid();
            GraphSettings graphSettings   = new GraphSettings();

            graphSettings.ApiVersion = GraphConfiguration.GraphApiVersion;
            GraphConnection graphConnection = new GraphConnection(accessToken, ClientRequestId, graphSettings);

            GraphObject graphContact = graphConnection.Get <Contact>(objectId);
            PagedResults <GraphObject> memberShip = graphConnection.GetLinkedObjects(graphContact, LinkProperty.MemberOf, null, 999);

            // filter for Groups only
            int           count           = 0;
            IList <Group> groupMembership = new List <Group>();

            foreach (GraphObject graphObj in memberShip.Results)
            {
                if (graphObj.ODataTypeName.Contains("Group"))
                {
                    Group groupMember = (Group)memberShip.Results[count];
                    groupMembership.Add(groupMember);
                }
                ++count;
            }

            return(View(groupMembership));
        }
コード例 #13
0
        /// <summary>
        /// Gets a list of <see cref="User"/> objects that are members of a give <see cref="Group"/>.
        /// </summary>
        /// <param name="objectId">Unique identifier of the <see cref="Group"/>.</param>
        /// <returns>A view with the list of <see cref="User"/> objects.</returns>
        public ActionResult GetMembers(string objectId)
        {
            //Get the access token as we need it to make a call to the Graph API
            string accessToken = AuthUtils.GetAuthToken(Request, HttpContext);

            if (accessToken == null)
            {
                //
                // The user needs to re-authorize.  Show them a message to that effect.
                //
                ViewBag.ErrorMessage = "AuthorizationRequired";
                return(View());
            }

            // Setup Graph API connection and get Group members
            Guid          ClientRequestId = Guid.NewGuid();
            GraphSettings graphSettings   = new GraphSettings();

            graphSettings.ApiVersion = GraphConfiguration.GraphApiVersion;
            GraphConnection graphConnection = new GraphConnection(accessToken, ClientRequestId, graphSettings);

            Group group = graphConnection.Get <Group>(objectId);
            PagedResults <GraphObject> members = graphConnection.GetLinkedObjects(group, LinkProperty.Members, null, 999);

            IList <User> users = new List <User>();

            foreach (GraphObject obj in members.Results)
            {
                if (obj is User)
                {
                    users.Add((User)obj);
                }
            }

            return(View(users));
        }