Exemple #1
0
 public override void OnException(HttpActionExecutedContext actionExecutedContext)
 {
     if (actionExecutedContext.Exception is UnauthorizedAccessException)
     {
         string resource         = ConfigurationManager.AppSettings["ida:Resource"];
         string redirectUri      = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority).ToString() + "/Home/SPA";
         string authorizationUrl = OAuthController.GetAuthorizationUrl(resource, new Uri(redirectUri));
         actionExecutedContext.Response = new HttpResponseMessage(HttpStatusCode.Unauthorized);
         System.Net.Http.Headers.AuthenticationHeaderValue realm = new System.Net.Http.Headers.AuthenticationHeaderValue("OAuth", "realm=\"" + authorizationUrl + "\"");
         actionExecutedContext.Response.Headers.WwwAuthenticate.Add(realm);
     }
 }
        public async Task <ActionResult> Index()
        {
            //OAuth Controller
            string resourceId       = ConfigurationManager.AppSettings["ida:ResourceId"];
            string accessToken      = null;
            string redirectUri      = null;
            string authorizationUrl = null;
            string tenantId         = (string)OAuthController.GetFromCache("TenantId");

            if (tenantId != null)
            {
                accessToken = OAuthController.GetAccessTokenFromCacheOrRefreshToken(tenantId, resourceId);
            }

            if (accessToken == null)
            {
                redirectUri      = this.Request.Url.GetLeftPart(UriPartial.Authority).ToString() + "/Home";
                authorizationUrl = OAuthController.GetAuthorizationUrl(resourceId, Request);
                OAuthController.SaveInCache("RedirectTo", new Uri(redirectUri));
                return(new RedirectResult(authorizationUrl));
            }

            //Search Query Uri
            StringBuilder requestUri = new StringBuilder(resourceId)
                                       .Append("/O3652-7/_api/web/lists/getbytitle('Contacts')/items");

            //Execute Query
            HttpClient         client  = new HttpClient();
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, requestUri.ToString());

            request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
            HttpResponseMessage response = await client.SendAsync(request);

            string json = await response.Content.ReadAsStringAsync();

            //Parse JSON
            JObject jsonObject = JObject.Parse(json);

            List <Person> people = new List <Person>();

            foreach (var p in jsonObject["value"])
            {
                string lastName  = p["Title"].Value <string>();
                string firstName = p["FirstName"].Value <string>();
                string jobTitle  = p["JobTitle"].Value <string>();
                string workEmail = p["EMail"].Value <string>();
                string workPhone = p["WorkPhone"].Value <string>();
                if (lastName != null)
                {
                    people.Add(new Person()
                    {
                        LastName  = lastName,
                        FirstName = firstName,
                        JobTitle  = jobTitle,
                        WorkEmail = workEmail,
                        WorkPhone = workPhone
                    });
                }
            }

            ViewBag.People = people;
            return(View());
        }