Пример #1
0
        /// <summary>
        /// The get user data.
        /// </summary>
        /// <param name="accessToken">
        /// The access token.
        /// </param>
        /// <returns>A dictionary of profile data.</returns>
        protected override NameValueCollection GetUserData(string accessToken)
        {
            var userData = new NameValueCollection();

            try {
                AzureADGraph graphData;
                WebRequest   request =
                    WebRequest.Create(
                        GraphEndpoint + this.tenantid + "/users/" + this.userid + "?api-version=2013-04-05");
                request.Headers = new WebHeaderCollection();
                request.Headers.Add("authorization", accessToken);
                using (var response = request.GetResponse()) {
                    using (var responseStream = response.GetResponseStream()) {
                        graphData = JsonHelper.Deserialize <AzureADGraph>(responseStream);
                    }
                }

                // this dictionary must contains
                userData.AddItemIfNotEmpty("id", graphData.ObjectId);
                userData.AddItemIfNotEmpty("username", graphData.UserPrincipalName);
                userData.AddItemIfNotEmpty("name", graphData.DisplayName);

                return(userData);
            } catch (Exception e) {
                System.Diagnostics.Debug.WriteLine(e.ToStringDescriptive());
                return(userData);
            }
        }
Пример #2
0
        /// <summary>
        /// Gets the extra data obtained from the response message when authentication is successful.
        /// </summary>
        /// <param name="response">
        /// The response message.
        /// </param>
        /// <returns>A dictionary of profile data; or null if no data is available.</returns>
        protected override NameValueCollection GetExtraData(IAuthenticationResponse response)
        {
            FetchResponse fetchResponse = response.GetExtension <FetchResponse>();

            if (fetchResponse != null)
            {
                var extraData = new NameValueCollection();
                extraData.AddItemIfNotEmpty("email", fetchResponse.GetAttributeValue(WellKnownAttributes.Contact.Email));
                extraData.AddItemIfNotEmpty("fullName", fetchResponse.GetAttributeValue(WellKnownAttributes.Name.FullName));

                return(extraData);
            }

            return(null);
        }
Пример #3
0
        /// <summary>
        /// Adds the value from an XDocument with the specified element name if it's not empty.
        /// </summary>
        /// <param name="dictionary">
        /// The dictionary.
        /// </param>
        /// <param name="document">
        /// The document.
        /// </param>
        /// <param name="elementName">
        /// Name of the element.
        /// </param>
        internal static void AddDataIfNotEmpty(
            this NameValueCollection dictionary, XDocument document, string elementName)
        {
            var element = document.Root.Element(elementName);

            if (element != null)
            {
                dictionary.AddItemIfNotEmpty(elementName, element.Value);
            }
        }
Пример #4
0
        /// <summary>
        /// Given the access token, gets the logged-in user's data. The returned dictionary must include two keys 'id', and 'username'.
        /// </summary>
        /// <param name="accessToken">
        /// The access token of the current user.
        /// </param>
        /// <returns>
        /// A dictionary contains key-value pairs of user data
        /// </returns>
        protected override NameValueCollection GetUserData(string accessToken)
        {
            MicrosoftClientUserData graph;
            var request =
                WebRequest.Create(
                    "https://apis.live.net/v5.0/me?access_token=" + MessagingUtilities.EscapeUriDataStringRfc3986(accessToken));

            using (var response = request.GetResponse()) {
                using (var responseStream = response.GetResponseStream()) {
                    graph = JsonHelper.Deserialize <MicrosoftClientUserData>(responseStream);
                }
            }

            var userData = new NameValueCollection();

            userData.AddItemIfNotEmpty("id", graph.Id);
            userData.AddItemIfNotEmpty("username", graph.Name);
            userData.AddItemIfNotEmpty("name", graph.Name);
            userData.AddItemIfNotEmpty("link", graph.Link == null ? null : graph.Link.AbsoluteUri);
            userData.AddItemIfNotEmpty("gender", graph.Gender);
            userData.AddItemIfNotEmpty("firstname", graph.FirstName);
            userData.AddItemIfNotEmpty("lastname", graph.LastName);
            return(userData);
        }
Пример #5
0
        /// <summary>
        /// The get user data.
        /// </summary>
        /// <param name="accessToken">
        /// The access token.
        /// </param>
        /// <returns>A dictionary of profile data.</returns>
        protected override NameValueCollection GetUserData(string accessToken)
        {
            FacebookGraphData graphData;
            var request =
                WebRequest.Create(
                    "https://graph.facebook.com/me?access_token=" + MessagingUtilities.EscapeUriDataStringRfc3986(accessToken));

            using (var response = request.GetResponse()) {
                using (var responseStream = response.GetResponseStream()) {
                    graphData = JsonHelper.Deserialize <FacebookGraphData>(responseStream);
                }
            }

            // this dictionary must contains
            var userData = new NameValueCollection();

            userData.AddItemIfNotEmpty("id", graphData.Id);
            userData.AddItemIfNotEmpty("username", graphData.Email);
            userData.AddItemIfNotEmpty("name", graphData.Name);
            userData.AddItemIfNotEmpty("link", graphData.Link == null ? null : graphData.Link.AbsoluteUri);
            userData.AddItemIfNotEmpty("gender", graphData.Gender);
            userData.AddItemIfNotEmpty("birthday", graphData.Birthday);
            return(userData);
        }