Example #1
0
		protected override AuthenticationResult VerifyAuthenticationCore(AuthorizedTokenResponse response) {
			string accessToken = response.AccessToken;
			string userId = response.ExtraData["user_id"];
			string userName = response.ExtraData["screen_name"];

			var profileRequestUrl = new Uri("https://api.twitter.com/1/users/show.xml?user_id="
									   + MessagingUtilities.EscapeUriDataStringRfc3986(userId));
			var profileEndpoint = new MessageReceivingEndpoint(profileRequestUrl, HttpDeliveryMethods.GetRequest);
			HttpWebRequest request = this.WebWorker.PrepareAuthorizedRequest(profileEndpoint, accessToken);

			var extraData = new Dictionary<string, string>();
			extraData.Add("accesstoken", accessToken);
			try {
				using (WebResponse profileResponse = request.GetResponse()) {
					using (Stream responseStream = profileResponse.GetResponseStream()) {
						XDocument document = LoadXDocumentFromStream(responseStream);
						extraData.AddDataIfNotEmpty(document, "name");
						extraData.AddDataIfNotEmpty(document, "location");
						extraData.AddDataIfNotEmpty(document, "description");
						extraData.AddDataIfNotEmpty(document, "url");
					}
				}
			}
			catch (Exception) {
				// At this point, the authentication is already successful.
				// Here we are just trying to get additional data if we can.
				// If it fails, no problem.
			}

			return new AuthenticationResult(
				isSuccessful: true, provider: this.ProviderName, providerUserId: userId, userName: userName, extraData: extraData);
		}
Example #2
0
		protected override AuthenticationResult VerifyAuthenticationCore(AuthorizedTokenResponse response) {
			// See here for Field Selectors API http://developer.linkedin.com/docs/DOC-1014
			const string profileRequestUrl =
				"http://api.linkedin.com/v1/people/~:(id,first-name,last-name,headline,industry,summary)";

			string accessToken = response.AccessToken;

			var profileEndpoint = new MessageReceivingEndpoint(profileRequestUrl, HttpDeliveryMethods.GetRequest);
			HttpWebRequest request = this.WebWorker.PrepareAuthorizedRequest(profileEndpoint, accessToken);

			try {
				using (WebResponse profileResponse = request.GetResponse()) {
					using (Stream responseStream = profileResponse.GetResponseStream()) {
						XDocument document = XDocument.Load(responseStream);
						string userId = document.Root.Element("id").Value;

						string firstName = document.Root.Element("first-name").Value;
						string lastName = document.Root.Element("last-name").Value;
						string userName = firstName + " " + lastName;

						var extraData = new Dictionary<string, string>();
						extraData.Add("accesstoken", accessToken);
						extraData.Add("name", userName);
						extraData.AddDataIfNotEmpty(document, "headline");
						extraData.AddDataIfNotEmpty(document, "summary");
						extraData.AddDataIfNotEmpty(document, "industry");

						return new AuthenticationResult(
							isSuccessful: true, provider: this.ProviderName, providerUserId: userId, userName: userName, extraData: extraData);
					}
				}
			} catch (Exception exception) {
				return new AuthenticationResult(exception);
			}
		}