예제 #1
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 IDictionary <string, string> GetUserData(string accessToken)
        {
            OAuthMicrosoftClientData data;

            using (var response = WebRequest.Create(UserDataEndpoint + HttpUtility.UrlEncode(accessToken)).GetResponse())
            {
                using (var stream = response.GetResponseStream())
                {
                    data = OAuthHelpers.Deserialize <OAuthMicrosoftClientData>(stream);
                }
            }

            // Try to select an email address for the user. :S
            string email = data.Emails.Preferred ?? data.Emails.Account ?? data.Emails.Personal ?? data.Emails.Business;

            string birthday;

            try
            {
                birthday = new DateTime(data.BirthYear, data.BirthMonth, data.BirthDay).ToString();
            }
            catch (ArgumentOutOfRangeException)
            {
                birthday = null;
            }

            return(OAuthHelpers.CreateResponse(data.Id, email, data.Name, data.FirstName, data.LastName, data.Gender, data.Link, birthday));
        }
예제 #2
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 IDictionary <string, string> GetUserData(string accessToken)
        {
            OAuthFacebookClientData data;

            using (var response = WebRequest.Create(UserDataEndpoint + HttpUtility.UrlEncode(accessToken)).GetResponse())
            {
                using (var stream = response.GetResponseStream())
                {
                    data = OAuthHelpers.Deserialize <OAuthFacebookClientData>(stream);
                }
            }

            return(OAuthHelpers.CreateResponse(data.Id, data.Email, data.Name, data.FirstName, data.LastName, data.Gender, data.Link, data.Birthday));
        }
예제 #3
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 IDictionary <string, string> GetUserData(string accessToken)
        {
            var builder = new UriBuilder(UserDataEndpoint);

            builder.SetQuery(new UriParameter("access_token", accessToken));

            OAuthGoogleClientData data;

            using (var response = WebRequest.Create(builder.Uri).GetResponse())
            {
                using (var stream = response.GetResponseStream())
                {
                    data = OAuthHelpers.Deserialize <OAuthGoogleClientData>(stream);
                }
            }

            return(OAuthHelpers.CreateResponse(data.Id, data.Email, data.Name, data.FirstName, data.LastName, data.Gender, data.Link, data.Birthday));
        }
예제 #4
0
        /// <summary>
        /// Queries the access token from the specified authorization code.
        /// </summary>
        /// <param name="returnUrl">The return URL.</param>
        /// <param name="authorizationCode">The authorization code.</param>
        /// <returns>
        /// The access token
        /// </returns>
        protected override string QueryAccessToken(Uri returnUrl, string authorizationCode)
        {
            string formData = UriUtility.CreateQueryString(
                new UriParameter("client_id", _appId),
                new UriParameter("client_secret", _appSecret),
                new UriParameter("code", authorizationCode),
                new UriParameter("grant_type", "authorization_code"),
                new UriParameter("redirect_uri", returnUrl.GetLeftPart(UriPartial.Path))); // Google OAuth 2 doesn't like query parameters.

            var request = WebRequest.Create(TokenEndpoint);

            request.ContentType   = "application/x-www-form-urlencoded";
            request.ContentLength = formData.Length;
            request.Method        = "POST";

            using (var stream = request.GetRequestStream())
            {
                var writer = new StreamWriter(stream);
                writer.Write(formData);
                writer.Flush();
            }

            var response = (HttpWebResponse)request.GetResponse();

            if (response.StatusCode == HttpStatusCode.OK)
            {
                using (var stream = response.GetResponseStream())
                {
                    var data = OAuthHelpers.Deserialize <OAuth2AccessTokenData>(stream);
                    if (data != null)
                    {
                        return(data.AccessToken);
                    }
                }
            }
            return(null);
        }