Пример #1
0
        // GET: Employees
        public ActionResult Index()
        {
            //get the token from the cache.
            string signedInUserID = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value;

            ADALTokenCache UserTokenCache = new ADALTokenCache(signedInUserID);

            List<TokenCacheItem> items = UserTokenCache.ReadItems().Where(tc => tc.Resource == "https://graph.microsoft.com").ToList();

            string myAccessToken = items[0].AccessToken;

            //make a HTTPClient request for the information and include the token.
            // thanks to post Vardhaman Deshpande.
            string resourceUrl = string.Format("https://graph.microsoft.com/beta/{0}/users", ConfigurationManager.AppSettings["ida:Domain"]);
            List<Employee> json = new List<Employee>();

            using (var client = new HttpClient())
            {
                using (var request = new HttpRequestMessage(HttpMethod.Get, resourceUrl))
                {
                    request.Headers.Add("Authorization", "Bearer " + myAccessToken);
                    request.Headers.Add("Accept", "application/json;odata.metadata=minimal");
                    using (var response = client.SendAsync(request).Result)
                    {
                        var jsonResult = JObject.Parse(response.Content.ReadAsStringAsync().Result);
                        json = JsonConvert.DeserializeObject<List<Employee>>(jsonResult["value"].ToString());
                    }
                }
            }

            return View(json.AsEnumerable<Employee>());
        }
Пример #2
0
        // GET: User
        public ActionResult Index()
        {
            //get the token from the cache.
            string signedInUserID = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value;

            ADALTokenCache UserTokenCache = new ADALTokenCache(signedInUserID);

            List<TokenCacheItem> items = UserTokenCache.ReadItems().Where(tc => tc.Resource == "https://graph.microsoft.com").ToList();

            string myAccessToken = items[0].AccessToken;

            //make a HTTPClient request for the information and include the token.
            // thanks to post Vardhaman Deshpande.
            string resourceUrl = "https://graph.microsoft.com/beta/me";

            using (var client = new HttpClient())
            {
                using (var request = new HttpRequestMessage(HttpMethod.Get, resourceUrl))
                {
                    request.Headers.Add("Authorization", "Bearer " + myAccessToken);
                    request.Headers.Add("Accept", "application/json;odata.metadata=minimal");
                    using (var response = client.SendAsync(request).Result)
                    {
                        var json = JObject.Parse(response.Content.ReadAsStringAsync().Result);

                        ViewBag.Name = json.ToString();
                    }
                }
            }

            return View();
        }