Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Net;
- using System.Net.Http;
- using System.Net.Http.Headers;
- using System.Text.RegularExpressions;
- using System.Threading.Tasks;
- /*
- * Creator: Dialog
- *
- * Contact:
- * E-mail: Relekka(at)outlook .com
- * XMPP: [email protected]
- *
- * Created: 09.22.2014
- */
- namespace ConsoleApplication1
- {
- /// <summary>
- /// Validates Facebook credentials
- /// </summary>
- public static class Facebook
- {
- private static HttpClient _client;
- /// <summary>
- /// Asynchronously tries to sign in to Facebook
- /// </summary>
- /// <param name="creds">The credentials to sign in to. Enter an e-mail address for the username</param>
- /// <returns>A boolean indicating success. False if something went wrong or the credentials are invalid</returns>
- public static async Task<bool> TrySignInAsync(NetworkCredential creds)
- {
- const string signInUrl = "https://www.facebook.com/login.php?login_attempt=1";
- var tokens = await GetTokenAsync();
- using (var message = new HttpRequestMessage(HttpMethod.Post, new Uri(signInUrl)))
- {
- message.Content = new FormUrlEncodedContent(new Dictionary<string, string>
- {
- { "lsd", tokens.Item1 },
- { "email", creds.UserName },
- { "pass", creds.Password },
- { "default_persistent", "0" },
- { "timezone", "-120" },
- { "lgnrnd", tokens.Item2 },
- { "lgnjs", UnixTime().ToString() },
- { "locale", "nl_NL" }
- });
- message.Content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
- var response = await _client.SendAsync(message);
- var responseContent = await response.Content.ReadAsStringAsync();
- return responseContent.IndexOf("<input class=\"uiLinkButtonInput\"", StringComparison.Ordinal) != -1;
- }
- }
- /// <summary>
- /// Asynchronously gets Facebook's authenticity tokens
- /// </summary>
- /// <returns>A tuple with the lsd token as its first item, and lgnrnd as its second</returns>
- private static async Task<Tuple<string, string>> GetTokenAsync()
- {
- _client = new HttpClient(new HttpClientHandler() { CookieContainer = new CookieContainer() }, true);
- _client.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:32.0) Gecko/20100101 Firefox/32.0");
- var pageSource = await _client.GetStringAsync(new Uri("https://facebook.com"));
- return ParseTokens(pageSource);
- }
- /// <summary>
- /// Parses Facebook tokens from a page source
- /// </summary>
- private static Tuple<string, string> ParseTokens(string pageSource)
- {
- const string lsdPattern = "(\\[\"LSD\",\\[\\]).*?(\"token\").*?(\".*?\")";
- const string lgnrndPattern = ".*?(\"lgnrnd\").*?(value).*?(\".*?\")";
- var lsdMatch = Regex.Match(pageSource, lsdPattern);
- var lgnrndMatch = Regex.Match(pageSource, lgnrndPattern);
- if (!(lsdMatch.Success && lgnrndMatch.Success))
- {
- throw new WebException("Could not get tokens.");
- }
- var trimChars = new[] { '\\', '"' };
- var lsdToken = lsdMatch.Groups[3].Value.Trim(trimChars);
- var lgnrndToken = lgnrndMatch.Groups[3].Value.Trim(trimChars);
- var result = new Tuple<string, string>(lsdToken, lgnrndToken);
- return result;
- }
- /// <summary>
- /// Gets the unix epoch time
- /// Credits: Ben Cull @ stackoverflow.com
- /// </summary>
- /// <returns>Unix epoch time in seconds</returns>
- private static long UnixTime()
- {
- var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
- return Convert.ToInt64((DateTime.Now.ToUniversalTime() - epoch).TotalSeconds);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment