Пример #1
0
        public static HttpClient GetHttpClientWithToken(CrmConnectionSettings settings)
        {
            string token = null;

            using (var authClient = new HttpClient())
            {
                var content = new FormUrlEncodedContent(new[]
                {
                    new KeyValuePair <string, string>("grant_type", "client_credentials"),
                    new KeyValuePair <string, string>("client_id", settings.ClientId),
                    new KeyValuePair <string, string>("client_secret", settings.ClientSecret),
                    new KeyValuePair <string, string>("resource", settings.Resource),
                });

                var response     = authClient.PostAsync(settings.Authority, content);
                var decoded      = response.Result.Content.ReadAsStringAsync().Result;
                var authResponse = JsonConvert.DeserializeObject <AuthResponse>(decoded);

                token = authResponse.access_token;
            }

            var client = new HttpClient();

            client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);

            return(client);
        }
Пример #2
0
        private HttpClient GetHttpClientWithToken(CrmConnectionSettings settings)
        {
            string token = null;

            using (var authClient = new HttpClient())
            {
                var content = new FormUrlEncodedContent(new[]
                {
                    new KeyValuePair <string, string>("grant_type", "client_credentials"),
                    new KeyValuePair <string, string>("client_id", settings.ClientId),
                    new KeyValuePair <string, string>("client_secret", settings.ClientSecret),
                    new KeyValuePair <string, string>("resource", settings.Resource),
                });

                var response     = authClient.PostAsync(settings.Authority, content);
                var decoded      = response.Result.Content.ReadAsStringAsync().Result;
                var authResponse = JObject.Parse(decoded);

                token = authResponse.Property("access_token").Value.ToString();
            }

            var client = new HttpClient();

            client.BaseAddress = new Uri(settings.ApiUrl);
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);

            return(client);
        }