public static AuthorizationToken FromJson(string json)
 {
     var token = JObject.Parse(json);
     var result = new AuthorizationToken();
     result.AccessToken = token.Value<string>("access_token");
     result.TokenType = token.Value<string>("token_type");
     result.RefreshToken = token.Value<string>("refresh_token");
     result.Expiration = DateTime.UtcNow.AddMinutes(token.Value<int>("expires_in"));
     return result;
 }
Exemplo n.º 2
0
        protected async virtual Task <AuthorizationToken> ProcessAuthorizationTokenResponseAsync(HttpResponseMessage result)
        {
            var data = await result.Content.ReadAsStringAsync();

            if (result.Content.Headers.ContentType.MediaType.Equals("application/json"))
            {
                // json from body
                return(AuthorizationToken.FromJson(data));
            }
            else
            {
                // form-url-encoded from body
                var values = HttpUtility.ParseQueryString(data);
                return(AuthorizationToken.FromCollection(values));
            }
        }
Exemplo n.º 3
0
        protected async virtual Task <IEnumerable <Claim> > GetProfileClaimsAsync(AuthorizationToken token)
        {
            var url = this.ProfileUrl + "?access_token=" + token.AccessToken;

            HttpClient client = new HttpClient();
            var        result = await client.GetAsync(url);

            if (result.IsSuccessStatusCode)
            {
                var json = await result.Content.ReadAsStringAsync();

                var profile = Newtonsoft.Json.JsonConvert.DeserializeObject <Dictionary <string, object> >(json);
                return(GetClaimsFromProfile(profile));
            }

            return(null);
        }
Exemplo n.º 4
0
        protected override async Task<IEnumerable<Claim>> GetProfileClaimsAsync(AuthorizationToken token)
        {

            var url = this.ProfileUrl + ":(id,first-name,last-name)?oauth2_access_token=" + token.AccessToken;

            HttpClient client = new HttpClient();
            client.DefaultRequestHeaders.Add("x-li-format", "json");
            var result = await client.GetAsync(url);
            if (result.IsSuccessStatusCode)
            {
                var json = await result.Content.ReadAsStringAsync();
                var profile = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
                return GetClaimsFromProfile(profile);


            }
            return null;
        }
 public static AuthorizationToken FromCollection(NameValueCollection values)
 {
     var result = new AuthorizationToken();
     result.AccessToken = values["access_token"];
     result.TokenType = values["token_type"];
     result.RefreshToken = values["refresh_token"];
     var expires = values["expires_in"];
     int expiresInt;
     if (expires != null && Int32.TryParse(expires, out expiresInt))
     {
         result.Expiration = DateTime.UtcNow.AddMinutes(expiresInt);
     }
     else
     {
         expires = values["expires"];
         if (expires != null && Int32.TryParse(expires, out expiresInt))
         {
             result.Expiration = DateTime.UtcNow.AddMinutes(expiresInt);
         }
     }
     return result;
 }
        public static AuthorizationToken FromCollection(NameValueCollection values)
        {
            var result = new AuthorizationToken();

            result.AccessToken  = values["access_token"];
            result.TokenType    = values["token_type"];
            result.RefreshToken = values["refresh_token"];
            var expires = values["expires_in"];
            int expiresInt;

            if (expires != null && Int32.TryParse(expires, out expiresInt))
            {
                result.Expiration = DateTime.UtcNow.AddMinutes(expiresInt);
            }
            else
            {
                expires = values["expires"];
                if (expires != null && Int32.TryParse(expires, out expiresInt))
                {
                    result.Expiration = DateTime.UtcNow.AddMinutes(expiresInt);
                }
            }
            return(result);
        }
Exemplo n.º 7
0
        protected async virtual Task<IEnumerable<Claim>> GetProfileClaimsAsync(AuthorizationToken token)
        {
            var url = this.ProfileUrl + "?" + this.accessTokenParameterName + "=" + token.AccessToken;

            //add additional params
            if (additionalParams != null)
            {
                foreach (string key in additionalParams)
                {
                    url += string.Format("&{0}={1}", key, additionalParams[key]);
                }
            }

            HttpClient client = new HttpClient();
            var result = await client.GetAsync(url);
            if (result.IsSuccessStatusCode)
            {
                var json = await result.Content.ReadAsStringAsync();
                var profile = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
                return GetClaimsFromProfile(profile);
            }

            return null;
        }
Exemplo n.º 8
0
        protected async virtual Task<IEnumerable<Claim>> GetProfileClaimsAsync(AuthorizationToken token)
        {
            var url = this.ProfileUrl + "?access_token=" + token.AccessToken;
            
            HttpClient client = new HttpClient();
            var result = await client.GetAsync(url);
            if (result.IsSuccessStatusCode)
            {
                var json = await result.Content.ReadAsStringAsync();
                var profile = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
                return GetClaimsFromProfile(profile);
            }

            return null;
        }