Esempio n. 1
0
        public ActivationPolicy GetActivationPolicy(long deviceId)
        {
            ActivationPolicy activationPolicy = null;

            bearerToken = new Authentication().GetBearerToken(Scope.Activation, deviceId);
            var url = appSettingsReader.GetValue("serverBase", typeof(string)).ToString() + appSettingsReader.GetValue("actPolicyEndpoint", typeof(string)).ToString();

            using (var httpClient = new HttpClient())
            {
                httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", bearerToken.AccessToken);
                httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                httpClient.DefaultRequestHeaders.Add("X-ActivationId", appSettingsReader.GetValue("deviceActivationId", typeof(string)).ToString());

                Console.WriteLine(url);
                HttpResponseMessage response   = httpClient.GetAsync(url).Result;
                IJsonSerializer     serializer = new JsonNetSerializer();

                using (var responseStream = response.Content.ReadAsStreamAsync().Result)
                {
                    if (responseStream == null)
                    {
                        return(null);
                    }
                    using (var streamReader = new StreamReader(responseStream))
                    {
                        var responseContent = streamReader.ReadToEnd();
                        Console.WriteLine(responseContent);
                        activationPolicy = serializer.Deserialize <ActivationPolicy>(responseContent);
                    }
                }
            }
            return(activationPolicy);
        }
Esempio n. 2
0
        public BearerAccessToken GetBearerToken(Scope scope, long deviceId)
        {
            BearerAccessToken bearerToken = null;
            string            jwtToken    = GetJwtToken(scope, deviceId);

            Console.WriteLine($"JWT Token: {jwtToken}");

            var username = appSettingsReader.GetValue("username", typeof(string)).ToString();
            var password = appSettingsReader.GetValue("password", typeof(string)).ToString();
            var url      = appSettingsReader.GetValue("serverBase", typeof(string)).ToString() + appSettingsReader.GetValue("authEndpoint", typeof(string)).ToString();

            var data = new Dictionary <string, string>
            {
                { "grant_type", "client_credentials" },
                { "client_assertion_type", "urn:ietf:params:oauth:client-assertion-type:jwt-bearer" },
                { "client_assertion", jwtToken },
                { "scope", scope == Scope.Activation ? "oracle/iot/activation" : "" }
            }.AsEnumerable();

            using (var httpClient = new HttpClient())
            {
                var content = new FormUrlEncodedContent(data);
                content.Headers.Clear();
                content.Headers.Add("Content-Type", "application/x-www-form-urlencoded");

                String authHeader = System.Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(username + ":" + password));
                //httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", authHeader);
                httpClient.DefaultRequestHeaders.Add("Authorization", "Basic " + authHeader);
                httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));


                HttpResponseMessage response   = httpClient.PostAsync(url, content).Result;
                IJsonSerializer     serializer = new JsonNetSerializer();

                using (var responseStream = response.Content.ReadAsStreamAsync().Result)
                {
                    if (responseStream == null)
                    {
                        return(null);
                    }
                    using (var streamReader = new StreamReader(responseStream))
                    {
                        var responseContent = streamReader.ReadToEnd();
                        Console.WriteLine(responseContent);
                        bearerToken = serializer.Deserialize <BearerAccessToken>(responseContent);
                        Console.WriteLine($"Bearer {bearerToken.AccessToken}");
                    }
                }
            }

            return(bearerToken);
        }