Exemple #1
0
        /// <summary>
        /// Gets user information from an token result.
        /// </summary>
        /// <param name="tokenResult">The token result.</param>
        /// <returns>An instance of UserProfile contaning the user information.</returns>
        public UserProfile GetUserInfo(TokenResult tokenResult)
        {
            if (tokenResult == null)
            {
                throw new ArgumentNullException("tokenResult");
            }

            var jsonProfile = !string.IsNullOrEmpty(tokenResult.IdToken) ?
                              this.GetJsonProfileFromIdToken(tokenResult.IdToken) :
                              this.GetJsonProfileFromAccessToken(tokenResult.AccessToken);

            var ignoredProperties = new string[] { "iss", "sub", "aud", "exp", "iat" };
            var mappedProperties  = new string[]
            {
                "email",
                "family_name",
                "gender",
                "given_name",
                "locale",
                "name",
                "nickname",
                "picture",
                "user_id",
                "identities"
            };

            var userProfile  = JsonConvert.DeserializeObject <UserProfile>(jsonProfile);
            var responseData = JsonConvert.DeserializeObject <Dictionary <string, object> >(jsonProfile);

            userProfile.ExtraProperties = responseData != null?
                                          responseData.Keys.Where(x => !mappedProperties.Contains(x) && !ignoredProperties.Contains(x)).ToDictionary(x => x, x => responseData[x]) :
                                              new Dictionary <string, object>();

            // Convert JArray to string[]
            for (int i = 0; i < userProfile.ExtraProperties.Count; i++)
            {
                var item = userProfile.ExtraProperties.ElementAt(i);
                if (item.Value is JArray)
                {
                    var stringArray = ((JArray)item.Value).Select(v => v.ToString()).ToArray();
                    userProfile.ExtraProperties.Remove(item.Key);
                    userProfile.ExtraProperties.Add(item.Key, stringArray);
                }
            }

            if (string.IsNullOrEmpty(userProfile.UserId))
            {
                return(this.GetUserInfo(new TokenResult {
                    AccessToken = tokenResult.AccessToken
                }));
            }

            return(userProfile);
        }
Exemple #2
0
        /// <summary>
        /// Gets user information from an token result.
        /// </summary>
        /// <param name="tokenResult">The token result.</param>
        /// <returns>An instance of UserProfile contaning the user information.</returns>
        public UserProfile GetUserInfo(TokenResult tokenResult)
        {
            if (tokenResult == null)
            {
                throw new ArgumentNullException("tokenResult");
            }

            var jsonProfile = !string.IsNullOrEmpty(tokenResult.IdToken) ?
                              this.GetJsonProfileFromIdToken(tokenResult.IdToken) :
                              this.GetJsonProfileFromAccessToken(tokenResult.AccessToken);

            var userProfile = this.GetUserProfileFromJson(jsonProfile);

            if (string.IsNullOrEmpty(userProfile.UserId))
            {
                return(this.GetUserInfo(new TokenResult {
                    AccessToken = tokenResult.AccessToken
                }));
            }

            return(userProfile);
        }