Advertisement
Jerl

Account.cs

Oct 25th, 2015
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.75 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Net;
  7. using System.IO;
  8. using System.Security;
  9. using System.Text.RegularExpressions;
  10.  
  11. namespace Jerl.GelbooruActionablesLibrary
  12. {
  13. /// <summary>
  14. /// This class enables the user to log in to the site.
  15. /// </summary>
  16. public static class Account
  17. {
  18.  
  19. //provide access to the cookies generated
  20. private static CookieContainer cookieContainer;
  21.  
  22. //backing field
  23. private static Uri loginUri = new Uri("http://gelbooru.com/index.php?page=account&s=login&code=00");
  24.  
  25. /// <summary>
  26. /// provides external visibility to the login URI should it be needed
  27. /// </summary>
  28. public static Uri LoginUri
  29. {
  30. get { return loginUri; }
  31. }
  32.  
  33. /// <summary>
  34. /// Logs the user in to the site using the console.
  35. /// </summary>
  36. /// <returns>A CookieContainer with the user's login credentials</returns>
  37. public static CookieContainer LoginUi()
  38. {
  39. bool loginSuccess = false;
  40. do
  41. {
  42.  
  43. //prompt the user for login information
  44. Console.WriteLine("Input username");
  45. string username;
  46. do
  47. username = Console.ReadLine();
  48. while (username == "");
  49.  
  50. Console.WriteLine("Input password");
  51. string password;
  52. do
  53. password = GetPassword();
  54. while (password == "");
  55.  
  56. //clear the console once we have what we need
  57. Console.Clear();
  58.  
  59. Login(username, password, out loginSuccess);
  60.  
  61. if (!loginSuccess)
  62. Console.WriteLine("Login failed!");
  63. } while (!loginSuccess);
  64.  
  65. return cookieContainer;
  66. }
  67.  
  68. /// <summary>
  69. /// Logs in to the site using the provided credentials and verifies that the login succeeded.
  70. /// </summary>
  71. /// <param name="username">The username to log in with.</param>
  72. /// <param name="password">The password to log in with.</param>
  73. /// <param name="loginSuccess">A boolean that will be set to true if login succeeds or false if login fails.</param>
  74. /// <returns>A CookieContainer containing successful login credentials.</returns>
  75. public static CookieContainer Login(string username, string password, out bool loginSuccess)
  76. {
  77. //create a byte array out of our request
  78. byte[] post = new ASCIIEncoding().GetBytes(string.Format("user={0}&pass={1}&submit=Log+in", username, password));
  79.  
  80. //create the login request
  81. HttpWebRequest loginRequest = (HttpWebRequest)WebRequest.Create(LoginUri);
  82. loginRequest.Proxy = null;
  83. loginRequest.Method = "POST";
  84. loginRequest.AllowAutoRedirect = false;
  85. loginRequest.ContentType = "application/x-www-form-urlencoded";
  86. loginRequest.ContentLength = post.Length;
  87.  
  88. //post the data
  89. Stream postStream = loginRequest.GetRequestStream();
  90. postStream.Write(post, 0, post.Length);
  91. postStream.Close();
  92.  
  93. //create the cookie container
  94. loginRequest.CookieContainer = new CookieContainer();
  95.  
  96. loginRequest.Referer = "http://gelbooru.com/index.php?page=account&s=login&code=00";
  97.  
  98. //load the page
  99. try
  100. {
  101. HttpWebResponse loginResponse = (HttpWebResponse)loginRequest.GetResponse();
  102. Stream dumpData = loginResponse.GetResponseStream();
  103. Regex loginCheckRegex = new Regex("s=home");
  104. using (StreamReader dumpDataReader = new StreamReader(dumpData))
  105. {
  106. //string page = dumpDataReader.ReadToEnd();
  107. //loginSuccess = loginCheckRegex.IsMatch(page);
  108. dumpDataReader.ReadToEnd();
  109.  
  110. }
  111. loginSuccess = (loginResponse.Headers["Location"] == "index.php?page=account&s=home");
  112.  
  113. //update our field with the cookies we acquired
  114. cookieContainer = loginRequest.CookieContainer;
  115. }
  116. catch (WebException)
  117. {
  118. loginSuccess = false;
  119. }
  120. return cookieContainer;
  121. }
  122.  
  123. /// <summary>
  124. /// Fetches the user's password from the console
  125. /// </summary>
  126. /// <returns>The user's password</returns>
  127. public static string GetPassword()
  128. {
  129. string pwd = "";
  130. while (true)
  131. {
  132. ConsoleKeyInfo i = Console.ReadKey(true);
  133. if (i.Key == ConsoleKey.Enter)
  134. {
  135. break;
  136. }
  137. else if (i.Key == ConsoleKey.Backspace)
  138. {
  139. if (pwd.Length > 0)
  140. {
  141. pwd.Remove(pwd.Length - 1);
  142. Console.Write("\b \b");
  143. }
  144. }
  145. else
  146. {
  147. pwd = pwd.Insert(pwd.Length, i.KeyChar.ToString());
  148. Console.Write("*");
  149. }
  150. }
  151. return pwd;
  152. }
  153.  
  154. /// <summary>
  155. /// Verifies that a CookieContainer contains valid login credentials.
  156. /// </summary>
  157. /// <param name="cookies">A CookieContainer to check for valid Gelbooru login credentials.</param>
  158. /// <returns>true if the credentials are valid, false if they are not.</returns>
  159. public static bool LoginVerified(CookieContainer cookies)
  160. {
  161.  
  162. Regex loggedInRegex = new Regex("index.php?page=account&s=profile");
  163.  
  164. HttpWebRequest loginVerificationRequest = (HttpWebRequest)WebRequest.Create("http://gelbooru.com/index.php?page=account&s=home");
  165. loginVerificationRequest.CookieContainer = cookies;
  166.  
  167. try
  168. {
  169. HttpWebResponse loginVerificationResponse = (HttpWebResponse)loginVerificationRequest.GetResponse();
  170.  
  171. string page;
  172. using (StreamReader sr = new StreamReader(loginVerificationResponse.GetResponseStream()))
  173. {
  174. page = sr.ReadToEnd();
  175. }
  176.  
  177. if (Regex.IsMatch(page, "Logout"))
  178. return true;
  179. return false;
  180. }
  181. catch (WebException)
  182. {
  183. return false;
  184. }
  185. }
  186.  
  187. }
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement