Advertisement
Guest User

Untitled

a guest
Jan 29th, 2017
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.88 KB | None | 0 0
  1. public bool LogIn()
  2.         {
  3.             string fullAddress = PageUrl.Replace("https://", "").Replace("/", "");
  4.             string formData = "log=" + HttpUtility.UrlEncode(UserName) + "&pwd=" + HttpUtility.UrlEncode(PassWord) + "&rememberme=forever&wp-submit=Log+In&redirect_to=https%3A%2F%2F" + HttpUtility.UrlEncode(fullAddress) + "%2Fwp-admin%2F&testcookie=1";
  5.             string responseStr = null;
  6.  
  7. // https://couponsgearbest.wordpress.com/2017/01/29/how-to-log-in-to-wordpress-dashboard-using-c-sharp-programming/
  8.  
  9.             using (var client = new TcpClient(fullAddress, 443))
  10.             {
  11.                 using (var sslsStream = new SslStream(client.GetStream(), true, null, null, EncryptionPolicy.AllowNoEncryption))
  12.                 {
  13.                     sslsStream.AuthenticateAsClient(fullAddress, null, SslProtocols.Tls, false);
  14.  
  15.                     using (var writer = new StreamWriter(sslsStream))
  16.                     using (var reader = new StreamReader(sslsStream, Encoding.UTF8))
  17.                     {
  18.                         writer.AutoFlush = true;
  19.                         writer.WriteLine("POST " + PageUrl + "/wp-login.php HTTP/1.1");
  20.                         writer.WriteLine("Host: " + fullAddress);
  21.                         writer.WriteLine("Connection: keep-alive");
  22.                         writer.WriteLine("Content-Length: " + formData.Length);
  23.                         writer.WriteLine("Origin: " + PageUrl);
  24.                         writer.WriteLine("User-Agent: " + webHelper.userAgent);
  25.                         writer.WriteLine("Content-Type: application/x-www-form-urlencoded");
  26.                         writer.WriteLine("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
  27.                         writer.WriteLine("Referer: " + PageUrl + "/wp-login.php");
  28.                         writer.WriteLine("Accept-Language: pl");
  29.                         writer.WriteLine("Cookie: wordpress_test_cookie=WP+Cookie+check");
  30.                         writer.WriteLine();
  31.                         writer.WriteLine(formData);
  32.  
  33.                         responseStr = reader.ReadToEnd();
  34.                     }
  35.                 }
  36.             }
  37.  
  38.             List<String> cookieHeaders = new List<string>();
  39.             foreach (string header in responseStr.Split("\n\r".ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
  40.             {
  41.                 if (header.StartsWith("Set-Cookie"))
  42.                 {
  43.                     cookieHeaders.Add(header.Replace("Set-Cookie: ", ""));
  44.                 }
  45.             }
  46.  
  47.             CookieContainer jar = new CookieContainer();
  48.             foreach (string cook in cookieHeaders)
  49.             {
  50.                 string name, value, path, domain;
  51.                 name = value = path = domain = "";
  52.  
  53.                 string[] split = cook.Split(';');
  54.                 foreach (string part in split)
  55.                 {
  56.                     if (part.StartsWith(" path="))
  57.                     {
  58.                         path = part.Replace(" path=", "").Trim();
  59.                     }
  60.  
  61.                     if (!part.StartsWith(" path=") && !part.StartsWith(" domain=") && part.Contains("=") && !part.Contains("expires") && !part.Contains("Max-Age"))
  62.                     {
  63.                         name = part.Split('=')[0].Trim();
  64.                         value = part.Split('=')[1].Trim();
  65.                     }
  66.                 }
  67.  
  68.                 Cookie cookie = new Cookie(name, value, path, fullAddress);
  69.                 jar.Add(cookie);
  70.             }
  71.  
  72.             webHelper.SetCookieContainer(jar);
  73.             currentCookieJar = jar;
  74.             if (jar.GetCookieHeader(new Uri(PageUrl)).Length == 0) return false;
  75.  
  76.             string src = webHelper.DownloadStuff(PageUrl + "/wp-admin/");
  77.             if (!src.Contains("Dashboard")) return false;
  78.  
  79.             loggedIn = true;
  80.             return true;
  81.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement