Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public bool LogIn()
- {
- string fullAddress = PageUrl.Replace("https://", "").Replace("/", "");
- 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";
- string responseStr = null;
- // https://couponsgearbest.wordpress.com/2017/01/29/how-to-log-in-to-wordpress-dashboard-using-c-sharp-programming/
- using (var client = new TcpClient(fullAddress, 443))
- {
- using (var sslsStream = new SslStream(client.GetStream(), true, null, null, EncryptionPolicy.AllowNoEncryption))
- {
- sslsStream.AuthenticateAsClient(fullAddress, null, SslProtocols.Tls, false);
- using (var writer = new StreamWriter(sslsStream))
- using (var reader = new StreamReader(sslsStream, Encoding.UTF8))
- {
- writer.AutoFlush = true;
- writer.WriteLine("POST " + PageUrl + "/wp-login.php HTTP/1.1");
- writer.WriteLine("Host: " + fullAddress);
- writer.WriteLine("Connection: keep-alive");
- writer.WriteLine("Content-Length: " + formData.Length);
- writer.WriteLine("Origin: " + PageUrl);
- writer.WriteLine("User-Agent: " + webHelper.userAgent);
- writer.WriteLine("Content-Type: application/x-www-form-urlencoded");
- writer.WriteLine("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
- writer.WriteLine("Referer: " + PageUrl + "/wp-login.php");
- writer.WriteLine("Accept-Language: pl");
- writer.WriteLine("Cookie: wordpress_test_cookie=WP+Cookie+check");
- writer.WriteLine();
- writer.WriteLine(formData);
- responseStr = reader.ReadToEnd();
- }
- }
- }
- List<String> cookieHeaders = new List<string>();
- foreach (string header in responseStr.Split("\n\r".ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
- {
- if (header.StartsWith("Set-Cookie"))
- {
- cookieHeaders.Add(header.Replace("Set-Cookie: ", ""));
- }
- }
- CookieContainer jar = new CookieContainer();
- foreach (string cook in cookieHeaders)
- {
- string name, value, path, domain;
- name = value = path = domain = "";
- string[] split = cook.Split(';');
- foreach (string part in split)
- {
- if (part.StartsWith(" path="))
- {
- path = part.Replace(" path=", "").Trim();
- }
- if (!part.StartsWith(" path=") && !part.StartsWith(" domain=") && part.Contains("=") && !part.Contains("expires") && !part.Contains("Max-Age"))
- {
- name = part.Split('=')[0].Trim();
- value = part.Split('=')[1].Trim();
- }
- }
- Cookie cookie = new Cookie(name, value, path, fullAddress);
- jar.Add(cookie);
- }
- webHelper.SetCookieContainer(jar);
- currentCookieJar = jar;
- if (jar.GetCookieHeader(new Uri(PageUrl)).Length == 0) return false;
- string src = webHelper.DownloadStuff(PageUrl + "/wp-admin/");
- if (!src.Contains("Dashboard")) return false;
- loggedIn = true;
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement