/// <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)); }
/// <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)); }
/// <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)); }
/// <summary> /// Check if authentication succeeded after user is redirected back from the service provider. /// </summary> /// <param name="response">The response token returned from service provider</param> /// <returns> /// Authentication result /// </returns> protected override AuthenticationResult VerifyAuthenticationCore(AuthorizedTokenResponse response) { string userId = response.ExtraData["user_id"]; string userName = response.ExtraData["screen_name"]; var location = new Uri("https://api.twitter.com/1/users/show.xml?user_id=" + OAuthHelpers.EscapeUriDataStringRfc3986(userId)); var profileEndpoint = new MessageReceivingEndpoint(location, HttpDeliveryMethods.GetRequest); var request = base.WebWorker.PrepareAuthorizedRequest(profileEndpoint, response.AccessToken); var dictionary = new Dictionary <string, string>() { { "accesstoken", response.AccessToken } }; try { using (var response2 = request.GetResponse()) { using (var stream = response2.GetResponseStream()) { var document = LoadXDocumentFromStream(stream); var name = OAuthHelpers.ParseName(GetElementValue(document, "name")); dictionary.Add("name", name.FullName); dictionary.Add("firstName", name.FirstName); dictionary.Add("lastName", name.LastName); dictionary.Add("location", GetElementValue(document, "location")); dictionary.Add("description", GetElementValue(document, "description")); dictionary.Add("url", GetElementValue(document, "url")); } } } catch (Exception) { } return(new AuthenticationResult(true, base.ProviderName, userId, userName, dictionary)); }
/// <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); }