Esempio n. 1
0
        /// <summary>
        /// Gets the OD ata service.
        /// </summary>
        /// <returns>The OD ata service.</returns>
        /// <param name="url">URL.</param>
        public static async Task <string> GetODataService(String url)
        {
            try
            {
                //Refresh Token if expires
                if (Convert.ToDateTime(Settings.ExpiresTime) <= DateTime.Now)
                {
                    //Authenticate against ADFS and NW Gateway
                    oAuthLogin oauthlogin   = new oAuthLogin();
                    String     access_token = await oauthlogin.LoginUserAsync(Constants._user);

                    if (access_token == "" && access_token == Constants._userNotExistInNWGateway)
                    {
                        Console.WriteLine("Logout");
                    }
                }
                using (var client = new HttpClient())
                {
                    client.BaseAddress = new Uri(Constants._azureAPIMDEVBase + url);
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", Constants._access_token);
                    client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "4f4af597f5814a989297601aaa276fe4");
                    try
                    {
                        HttpResponseMessage response = await client.GetAsync(Constants._azureAPIMDEVBase + url);

                        using (HttpContent content = response.Content)
                        {
                            string result = await content.ReadAsStringAsync();

                            return(result);
                        }
                    }
                    catch (Exception)
                    {
                        return("Error");
                    }
                }
            }
            catch (Exception)
            {
                return("Error");
            }
        }
Esempio n. 2
0
        public async static Task <string> GetODataService(string url, string postBody)
        {
            string xcsrf_token  = "";
            string cookie_value = "";

            try
            {
                //Refresh Token if expires
                if (Convert.ToDateTime(Settings.ExpiresTime) <= DateTime.Now)
                {
                    //Authenticate against ADFS and NW Gateway
                    oAuthLogin oauthlogin = new oAuthLogin();

                    string access_token = await oauthlogin.LoginUserAsync(Constants._user);

                    if (access_token == "" && access_token == Constants._userNotExistInNWGateway)
                    {
                        Console.WriteLine("Logout");
                    }
                }

                CookieContainer   cookies = new CookieContainer();
                HttpClientHandler handler = new HttpClientHandler();
                handler.CookieContainer = cookies;

                using (var client = new HttpClient(handler))
                {
                    //Get X-CSRF-TOKEN
                    client.BaseAddress = new Uri(Constants._azureAPIMDEVBase + url);
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", Constants._access_token);
                    client.DefaultRequestHeaders.Add("X-CSRF-Token", "fetch");
                    client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "4f4af597f5814a989297601aaa276fe4");

                    //GET data
                    HttpResponseMessage response = await client.GetAsync(Constants._azureAPIMDEVBase + url);

                    xcsrf_token = response.Headers.GetValues("X-CSRF-Token").FirstOrDefault();

                    Uri uri = new Uri(Constants._azureAPIMDEVBase + url);
                    IEnumerable <Cookie> responseCookies = cookies.GetCookies(uri).Cast <Cookie>();
                    foreach (Cookie cookie in responseCookies)
                    {
                        if (Constants._cookie == cookie.Name)
                        {
                            cookie_value = cookie.Value;
                        }
                    }
                }

                if (xcsrf_token != "" && cookie_value != "")
                {
                    //Post Method
                    Uri baseUri = new Uri(Constants._azureAPIMDEVBase + url);
                    HttpClientHandler clientHandler = new HttpClientHandler();
                    //Set Cookie in Post
                    clientHandler.CookieContainer.Add(baseUri, new Cookie(Constants._cookie, cookie_value));

                    using (var client_post = new HttpClient(clientHandler))
                    {
                        client_post.BaseAddress = new Uri(Constants._azureAPIMDEVBase + url);
                        client_post.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", Constants._access_token);
                        client_post.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                        //Set Token in Post
                        client_post.DefaultRequestHeaders.Add("X-CSRF-Token", xcsrf_token);
                        client_post.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "4f4af597f5814a989297601aaa276fe4");

                        //Post json content
                        var response = client_post.PostAsync(Constants._azureAPIMDEVBase + url, new StringContent(postBody, Encoding.UTF8, "application/json")).Result;
                        if (response.IsSuccessStatusCode)
                        {
                            Debug.WriteLine(response);
                            return("success");
                        }
                        else
                        {
                            return(await response.Content.ReadAsStringAsync());
                        }
                    }
                }
                else
                {
                    return("Token or cookie is not available");
                }
            }
            catch (Exception e)
            {
                return("Error");
            }
        }