public static void ValidateAntiForgery(this HttpRequestMessage request)
        {
            string cookieToken = string.Empty;
            string formToken   = string.Empty;

            IEnumerable <string> tokenHeaders;

            if (request.Headers.TryGetValues("RequestVerificationToken", out tokenHeaders))
            {
                string[] tokens = tokenHeaders.First().Split(':');
                if (tokens.Length == 2)
                {
                    cookieToken = tokens[0].Trim();
                    formToken   = tokens[1].Trim();
                    OAuthController.SaveInCache("cookieToken", cookieToken);
                    OAuthController.SaveInCache("formToken", formToken);
                }
            }
            else
            {
                cookieToken = OAuthController.GetFromCache("cookieToken").ToString();
                formToken   = OAuthController.GetFromCache("formToken").ToString();
            }

            AntiForgery.Validate(cookieToken, formToken);
        }
        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());
        }
Esempio n. 3
0
 public XElement ToXElement()
 {
     return(new XElement(ExtensionMethods.atom + "entry",
                         new XAttribute(XNamespace.Xmlns + "d", ExtensionMethods.d),
                         new XAttribute(XNamespace.Xmlns + "m", ExtensionMethods.m),
                         new XElement(ExtensionMethods.atom + "category", new XAttribute("term", OAuthController.GetFromCache(this.ReferencesListName)), new XAttribute("scheme", "http://schemas.microsoft.com/ado/2007/08/dataservices/scheme")),
                         new XElement(ExtensionMethods.atom + "content", new XAttribute("type", "application/xml"),
                                      new XElement(ExtensionMethods.m + "properties",
                                                   new XElement(ExtensionMethods.d + "URL", new XAttribute(ExtensionMethods.m + "type", "SP.FieldUrlValue"),
                                                                new XElement(ExtensionMethods.d + "Description", this.Title),
                                                                new XElement(ExtensionMethods.d + "Url", this.Url)),
                                                   new XElement(ExtensionMethods.d + "Comments", this.Notes),
                                                   new XElement(ExtensionMethods.d + "Project", this.Project)))));
 }
 public XElement ToXElement()
 {
     return(new XElement(ExtensionMethods.atom + "entry",
                         new XAttribute(XNamespace.Xmlns + "d", ExtensionMethods.d),
                         new XAttribute(XNamespace.Xmlns + "m", ExtensionMethods.m),
                         new XElement(ExtensionMethods.atom + "category", new XAttribute("term", OAuthController.GetFromCache(this.ProjectsListName)), new XAttribute("scheme", "http://schemas.microsoft.com/ado/2007/08/dataservices/scheme")),
                         new XElement(ExtensionMethods.atom + "content", new XAttribute("type", "application/xml"),
                                      new XElement(ExtensionMethods.m + "properties",
                                                   new XElement(ExtensionMethods.d + "Title", this.Title)))));
 }