/// <summary>
        /// Get additional details about the user to help in investigating the alert
        /// </summary>
        /// <param name="principalName">User principal name</param>
        /// <param name="populatePicture"></param>
        /// <param name="populateManager"></param>
        /// <param name="populateDevices"></param>
        /// <returns>GraphUserModel</returns>
        public async Task <GraphUserModel> GetUserDetails(string principalName, bool populatePicture = false, bool populateManager = false, bool populateDevices = false)
        {
            GraphUserModel userModel = null;

            graphClient.BaseUrl = "https://graph.microsoft.com/beta";
            try
            {
                //var user = await graphClient.Users[principalName].Request().GetAsync();
                var user = await graphClient.Users.Request().Filter($"UserPrincipalName eq '{principalName}'").GetAsync();

                if (user.Count > 0)
                {
                    userModel = BuildGraphUserModel(user.CurrentPage[0]);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            if (userModel == null)
            {
                return(null);
            }

            try
            {
                if (populatePicture)
                {
                    var picture = await graphClient.Users[principalName].Photo.Content.Request().GetAsync();

                    if (picture != null)
                    {
                        MemoryStream picture1 = (MemoryStream)picture;
                        string       pic      = "data:image/png;base64," + Convert.ToBase64String(picture1.ToArray(), 0, picture1.ToArray().Length);
                        userModel.Picture = pic;
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            try
            {
                if (populateManager)
                {
                    var manager = await graphClient.Users[principalName].Manager.Request().GetAsync();
                    if (!string.IsNullOrEmpty(manager?.Id))
                    {
                        userModel.Manager = BuildGraphUserModel(await graphClient.Users[manager.Id].Request().GetAsync());
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            try
            {
                if (populateDevices)
                {
                    var devices = await graphClient.Users[principalName].RegisteredDevices.Request().GetAsync();
                    if (devices != null)
                    {
                        userModel.RegisteredDevices = await Task.WhenAll(devices.Select(d => GetDeviceById(d.Id)));
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            try
            {
                if (populateDevices)
                {
                    var devices = await graphClient.Users[principalName].OwnedDevices.Request().GetAsync();
                    if (devices != null)
                    {
                        userModel.OwnedDevices = await Task.WhenAll(devices.Select(d => GetDeviceById(d.Id)));
                    }
                }
            }
            catch { }

            graphClient.BaseUrl = ConfigurationManager.AppSettings["GraphBaseUrl"];
            return(userModel);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Get additional details about the user to help in investigating the alert
        /// </summary>
        /// <param name="principalName">User principal name</param>
        /// <param name="populatePicture"></param>
        /// <param name="populateManager"></param>
        /// <param name="populateDevices"></param>
        /// <returns>Graph User Model</returns>
        public async Task <GraphUserModel> GetUserDetails(string principalName, bool populatePicture = false, bool populateManager = false, bool populateDevices = false)
        {
            GraphUserModel userModel = null;

            this.graphClient.BaseUrl = this.GraphBetaUrl;
            try
            {
                var user = await this.graphClient.Users.Request().Filter($"UserPrincipalName eq '{principalName}'").GetAsync();

                if (user.Count > 0)
                {
                    userModel = BuildGraphUserModel(user.CurrentPage[0]);
                }
            }
            catch (Exception ex)
            {
                Trace.TraceError(ex.Message);
            }

            if (userModel == null)
            {
                return(null);
            }

            try
            {
                if (populatePicture)
                {
                    var picture = await this.graphClient.Users[principalName].Photo.Content.Request().GetAsync();

                    if (picture != null)
                    {
                        MemoryStream picture1 = (MemoryStream)picture;
                        string       pic      = "data:image/png;base64," + Convert.ToBase64String(picture1.ToArray(), 0, picture1.ToArray().Length);
                        userModel.Picture = pic;
                    }
                }
            }
            catch (Exception ex)
            {
                Trace.TraceError(ex.Message);
            }

            try
            {
                if (populateManager)
                {
                    var manager = await this.graphClient.Users[principalName].Manager.Request().GetAsync();
                    if (!string.IsNullOrEmpty(manager?.Id))
                    {
                        userModel.Manager = BuildGraphUserModel(await this.graphClient.Users[manager.Id].Request().GetAsync());
                    }
                }
            }
            catch (Exception ex)
            {
                Trace.TraceError(ex.Message);
            }

            try
            {
                if (populateDevices)
                {
                    var devices = await this.graphClient.Users[principalName].RegisteredDevices.Request().GetAsync();
                    if (devices != null)
                    {
                        userModel.RegisteredDevices = await Task.WhenAll(devices.Select(d => this.GetDeviceById(d.Id)));
                    }
                }
            }
            catch (Exception ex)
            {
                Trace.TraceError(ex.Message);
            }

            try
            {
                if (populateDevices)
                {
                    var devices = await this.graphClient.Users[principalName].OwnedDevices.Request().GetAsync();
                    if (devices != null)
                    {
                        userModel.OwnedDevices = await Task.WhenAll(devices.Select(d => this.GetDeviceById(d.Id)));
                    }
                }
            }
            catch
            {
            }

            this.graphClient.BaseUrl = this.GraphUrl;
            return(userModel);
        }