示例#1
0
        private static async Task <IEnumerable <string> > TokenAsyncReal(string pathAPI, string pathToken, string client_id, string RequestedScopes)
        {
            using (var client = new HttpClient())
            {
                //Define Headers
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer");

                //Prepare Request Body
                List <KeyValuePair <string, string> > requestData = new List <KeyValuePair <string, string> >();
                requestData.Add(new KeyValuePair <string, string>("grant_type", "client_credentials"));
                requestData.Add(new KeyValuePair <string, string>("client_id", client_id));
                requestData.Add(new KeyValuePair <string, string>("client_secret", "secret"));
                requestData.Add(new KeyValuePair <string, string>("redirect_uri", pathAPI));
                requestData.Add(new KeyValuePair <string, string>("response_type", "token"));
                requestData.Add(new KeyValuePair <string, string>("RequestedScopes", RequestedScopes));

                FormUrlEncodedContent requestBody = new FormUrlEncodedContent(requestData);

                //Request Token
                var request = await client.PostAsync(pathToken, requestBody);

                var response = request.Content.ReadAsStringAsync();
                var token    = JsonConvert.DeserializeObject <AccessToken>(response.Result.ToString());

                lsthub[client_id] = new csttk(token.access_token.ToString(), DateTime.Now.AddSeconds(token.expires_in));

                return(new string[] { token.access_token.ToString(), token.expires_in.ToString() });
            }
        }
示例#2
0
        private static async Task <IEnumerable <string> > TokenAsync(string pathAPI, string pathToken, string client_id, string RequestedScopes)
        {
            csttk mykey = (csttk)lsthub[client_id];

            if (mykey.token == "A")
            {
                IEnumerable <string> token = await TokenAsyncReal(pathAPI, pathToken, client_id, RequestedScopes);

                return(token);
            }
            else
            {
                if ((mykey.expires) <= DateTime.Now)
                {
                    IEnumerable <string> token = await TokenAsyncReal(pathAPI, pathToken, client_id, RequestedScopes);

                    return(token);
                }
                else
                {
                    return(new string[] { mykey.token, mykey.expires.ToString() });
                }
            }
        }