Advertisement
Guest User

Untitled

a guest
Jul 26th, 2018
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.66 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Net.Http;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using Xamarin.Forms;
  11.  
  12. namespace T4SHubMobile
  13. {
  14. public partial class MainPage : ContentPage
  15. {
  16. // Strings
  17. string Cookies = null;
  18.  
  19. // Ints
  20. int Redirections;
  21.  
  22. // Views
  23. ScrollView ContainerScrollView;
  24. StackLayout WrapperStackLayout;
  25. Image T4Sicon;
  26. Label HeaderLabel;
  27. Label ErrorTextLabel;
  28. Entry UsernameInput;
  29. Entry PasswordInput;
  30. Button LoginButton;
  31. WebView LoginWebView;
  32.  
  33. public MainPage()
  34. {
  35. InitializeComponent();
  36.  
  37. // Get references to views
  38. ContainerScrollView = this.FindByName<ScrollView>("containerScrollView");
  39. WrapperStackLayout = this.FindByName<StackLayout>("wrapperStackLayout");
  40. T4Sicon = this.FindByName<Image>("t4Sicon");
  41. HeaderLabel = this.FindByName<Label>("headerLabel");
  42. ErrorTextLabel = this.FindByName<Label>("errorTextLabel");
  43. UsernameInput = this.FindByName<Entry>("usernameInput");
  44. PasswordInput = this.FindByName<Entry>("passwordInput");
  45. LoginButton = this.FindByName<Button>("loginButton");
  46. LoginWebView = this.FindByName<WebView>("loginWebView");
  47.  
  48. // Init variables
  49. Redirections = 0;
  50.  
  51. // logout for testing
  52. LoginWebView.Source = "https://www.t4shub.com/Account/Signout";
  53. }
  54.  
  55. public void OnLoginButtonClick()
  56. {
  57. // Check if input fields are empty, if not then proceed to login
  58. if (!String.IsNullOrWhiteSpace(UsernameInput.Text) && !String.IsNullOrWhiteSpace(PasswordInput.Text))
  59. {
  60. App.Username = UsernameInput.Text;
  61. App.Password = PasswordInput.Text;
  62.  
  63. // Add OnPageFinished method to webview
  64. LoginWebView.Navigated += OnPageFinished;
  65.  
  66. // Login
  67. Redirections = 0;
  68. Login();
  69. }
  70. else
  71. {
  72. ErrorTextLabel.Text = "Username/Password cannot be left empty!";
  73. }
  74.  
  75. }
  76.  
  77. public void Login()
  78. {
  79. LoginWebView.Source = "https://www.t4shub.com/Account/SignIn";
  80. //Application.Current.MainPage = new NavigationPage(new SearchPage());
  81. }
  82.  
  83. public void OnPageFinished(object sender, WebNavigatedEventArgs e)
  84. {
  85. Debug.WriteLine("PAGE " + e.Url + " FINISHED LOADING!");
  86.  
  87. // If the login succeeds and the user is redirected to the t4shub homepage, go to search page
  88. if (e.Url.Contains("https://www.t4shub.com/") && e.Result == WebNavigationResult.Success)
  89. {
  90.  
  91. Debug.WriteLine("Url contains \"https://www.t4shub.com/\"");
  92. }
  93. else if (e.Url.Contains("login.microsoftonline") && Redirections == 2)
  94. {
  95. WebView view = sender as WebView;
  96. InjectScriptFile(view);
  97. }
  98. Redirections++; Debug.WriteLine("Redirections: " + Redirections);
  99. }
  100.  
  101. private void InjectScriptFile(WebView view)
  102. {
  103. var jscall = "var parent = document.getElementsByTagName('head').item(0);" + "\n" +
  104. "var script = document.createElement('script');" + "\n" +
  105. "script.type = 'text/javascript';" + "\n" +
  106. "var useridView = document.getElementById('cred_userid_inputtext');" + "\n" +
  107. "var passwordView = document.getElementById('cred_password_inputtext');" + "\n" +
  108. "var signInButton = document.getElementById('cred_sign_in_button');" + "\n" +
  109. "var userid = \"" + App.Username + "\";" + "\n" +
  110. "var password = \"" + App.Password + "\";" + "\n" +
  111. "useridView.value = userid;" + "\n" +
  112. "passwordView.value = password;" + "\n" +
  113. "signInButton.click();" + "\n" +
  114. "parent.appendChild(script);";
  115. try
  116. {
  117. view.Eval(jscall);
  118.  
  119. Debug.WriteLine("Javascript:\n\n" + jscall + "\n\nevaluated!");
  120. }
  121. catch (IOException e)
  122. {
  123. // TODO Auto-generated catch block
  124. Debug.WriteLine("JAVASCRIPT INJECTION ERROR: " + e);
  125. }
  126. }
  127. }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement